Computing.Net > Forums > Programming > Helps C++: Read data from files.

Helps C++: Read data from files.

Reply to Message Icon

Original Message
Name: Truong Son
Date: March 5, 2004 at 00:55:43 Pacific
Subject: Helps C++: Read data from files.
OS: winXP
CPU/Ram: PIV 2.6GHz - 512RAM
Comment:

Hi there,

I am trying to assign value (double) for an variable from a separate data file. however, it fails.

Here below is the code I wrote:

#include <iostream>
#include <fstream>

using std::ofstream;

using namespace std;
int Npoint=0;
double X[100];
double Y[100];
double Z[100];

main (){
/* Read in data from the specified
file. File format is pretty simple:
one integer that give the number of
point, then the X, Y, Z location of each.
In the Point.dat I store the data like this:
3
1.0 4.3 3.4
2.5 -3.2 4.2
1.5 1.2 -1.2


*/

ifstream inPoint("Point.dat", ios::in);
//exit program if can not open file.
if(!inPoint){
cerr << "File could not be opened." << endl;
exit(1);
}

inPoint >> Npoint >> endl;
for(int i=0; i<Npoint; i++)
inPoint >> X[i] >> Y[i] >> Z[i] >> endl;

inPoint.close();

return 0;

}

However, when I run the program give wrong results (only recognize Npoint correctly and give the wrong coordinates.)

Please help me to read the data correctly.
Thank you very much for your help.

Best Regards,

Truong Son


Best Regards,

Truong Son


Report Offensive Message For Removal


Response Number 1
Name: egkenny
Date: March 5, 2004 at 09:11:26 Pacific
Reply: (edit)

Your program ran fine for me after I removed the the endl's from your input statements. endl is for output streams not input ones.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Helps C++: Read data from files.

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes Today.
Discuss in The Lounge