reading data from a data file
-
Respected all Regards! how i can read data from a data file which has extenstion is *.Dat which is in the follwing format --------------------------- filename.dat data file genertaed from the processing of the control system XLOC YLOC INDIX 45 56 2344 56 67 1245 50 90 4562 45 87 9337 ----------------------------- and in the given data file i have to read only the data of columns under XLOC,YLOC,INDIX and the file always is in this format. So please guide me. waiting for + response. david
-
Respected all Regards! how i can read data from a data file which has extenstion is *.Dat which is in the follwing format --------------------------- filename.dat data file genertaed from the processing of the control system XLOC YLOC INDIX 45 56 2344 56 67 1245 50 90 4562 45 87 9337 ----------------------------- and in the given data file i have to read only the data of columns under XLOC,YLOC,INDIX and the file always is in this format. So please guide me. waiting for + response. david
Use an ifstream. Once you get past the first lines, declaring an int and using >> will work fine, so long as you have some error handling code if the file is corrupted. Christian Graus - Microsoft MVP - C++
-
Respected all Regards! how i can read data from a data file which has extenstion is *.Dat which is in the follwing format --------------------------- filename.dat data file genertaed from the processing of the control system XLOC YLOC INDIX 45 56 2344 56 67 1245 50 90 4562 45 87 9337 ----------------------------- and in the given data file i have to read only the data of columns under XLOC,YLOC,INDIX and the file always is in this format. So please guide me. waiting for + response. david
There are many ways to approach this. One, although not the most efficient, looks something like:
FILE *pFile;
int XLOC, YLOC, INDIX;
char szLine[128];
pFile = fopen("filename.dat", "rt");// discard the first six lines
fgets(szLine, sizeof(szLine), pFile);
fgets(szLine, sizeof(szLine), pFile);
fgets(szLine, sizeof(szLine), pFile);
fgets(szLine, sizeof(szLine), pFile);
fgets(szLine, sizeof(szLine), pFile);
fgets(szLine, sizeof(szLine), pFile);while (! feof(pFile))
fscanf(pFile, "%d %d %d", &XLOC, &YLOC, &INDIX);fclose(pFile);
A similar MFC solution exists.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
-
Respected all Regards! how i can read data from a data file which has extenstion is *.Dat which is in the follwing format --------------------------- filename.dat data file genertaed from the processing of the control system XLOC YLOC INDIX 45 56 2344 56 67 1245 50 90 4562 45 87 9337 ----------------------------- and in the given data file i have to read only the data of columns under XLOC,YLOC,INDIX and the file always is in this format. So please guide me. waiting for + response. david
Hello David If you think that you've got only half the answer you need... you are not the only one! Often I get very well intentioned replies but full with the assumption that we are all engineers... Anyway, here's what I use to read realtively small files. If reading the file takes more than a few minutes, the decision to use this method or any other depends on how many times during the execution of the program you have to read the file. If you only read it once, go check the scores of the latest soccer games while the computer works. If you need to read the file many times, then it is better to transform your data file into a binary file (if you need it I can also send you code for that). You'll have to wait once while the file is converted but once your data is in binary, reading it from your program is lightning fast. OK, here's the code to read a text file regardless of the file extension. Note that I made the code general so that the first header lines can be one or more. You can choose to change the cin inputs if you know in advance what the will always be (i.e. one header line). Also, please note that the data being read by this program is comma separated. If you don't have commas, use the next commented line. If your data files have a variable amount of lines (e.g. you don't always have the same amount of rows) use a vector container to store your data. Don't hesitate to ask if you have more questions. #include #include using namespace std;
int main() { char stuff[100]; int period, stufflines; double dat1, dat2, dat3, dat4, integ, fraction; char separator; int counter; cout << " " << endl; cout << "how many lines with stuff at the beginning?" << endl; cin >> stufflines; cout << " " << endl; ifstream FileIn("C:\\SampleData\\divedata.dat"); if(!FileIn.is_open()) cout << "Could not open the file! Check directory..." << endl; else cout << "You're good, input file opened!" << endl; counter = 1; while(!FileIn.eof()) { if(counter <= stufflines) FileIn.getline (stuff, 100); else { FileIn >> dat1 >> separator >> dat2 >> separator >> dat3 >> separator >> dat4; // FileIn >> dat1 >> dat2 >> dat3 >> dat4; // if no commas } counter++; } return 0; }
Good luck Carlos -
Hello David If you think that you've got only half the answer you need... you are not the only one! Often I get very well intentioned replies but full with the assumption that we are all engineers... Anyway, here's what I use to read realtively small files. If reading the file takes more than a few minutes, the decision to use this method or any other depends on how many times during the execution of the program you have to read the file. If you only read it once, go check the scores of the latest soccer games while the computer works. If you need to read the file many times, then it is better to transform your data file into a binary file (if you need it I can also send you code for that). You'll have to wait once while the file is converted but once your data is in binary, reading it from your program is lightning fast. OK, here's the code to read a text file regardless of the file extension. Note that I made the code general so that the first header lines can be one or more. You can choose to change the cin inputs if you know in advance what the will always be (i.e. one header line). Also, please note that the data being read by this program is comma separated. If you don't have commas, use the next commented line. If your data files have a variable amount of lines (e.g. you don't always have the same amount of rows) use a vector container to store your data. Don't hesitate to ask if you have more questions. #include #include using namespace std;
int main() { char stuff[100]; int period, stufflines; double dat1, dat2, dat3, dat4, integ, fraction; char separator; int counter; cout << " " << endl; cout << "how many lines with stuff at the beginning?" << endl; cin >> stufflines; cout << " " << endl; ifstream FileIn("C:\\SampleData\\divedata.dat"); if(!FileIn.is_open()) cout << "Could not open the file! Check directory..." << endl; else cout << "You're good, input file opened!" << endl; counter = 1; while(!FileIn.eof()) { if(counter <= stufflines) FileIn.getline (stuff, 100); else { FileIn >> dat1 >> separator >> dat2 >> separator >> dat3 >> separator >> dat4; // FileIn >> dat1 >> dat2 >> dat3 >> dat4; // if no commas } counter++; } return 0; }
Good luck CarlosThanks Sir david