read a certain line from a text file
-
YOU HAVE GOT A PROBLEM IN THE
while
LOOP. I have stripped down ur while loop for simplicity;) The Problem(step by step):while (!GPSFile.eof()){ GPSFile >> gpsx; GPSFile >> gpsy; }
In the first time it loops, x-value is copied into gpsx and y-value is copied into gpsy. BUT you havent reached end of file yet, SO thewhile
loop is executed again and this time, the value of speed is copied into gspx (thats the ERROR!!!) So erronous output is 2.8 4.87067e+006 SOLUTION Solution 1:while (!GPSFile.eof()){ if (GPSFile.rdstate() == ios::failbit){cout<<"ERROR, \n";} GPSFile >> gpsx; GPSFile >> gpsy; break; **//BREAK OUT SO THAT THE WHILE LOOP IS NOT EXECUTED AGAIN** :rose: }//endwhile CataCoordX=gpsx; CataCoordY=gpsy;
Solution 2:while (!GPSFile.eof()){ if (GPSFile.rdstate() == ios::failbit){cout<<"ERROR, \n";} GPSFile >> gpsx; GPSFile >> gpsy; GPSFile >> gpspeed;//on lit **//THIS WILL END READING THE FILE(reach EOF)**:rose: CataCoordX=gpsx; CataCoordY=gpsy; }//endwhile
PROBLEM SOLVED Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs :) ...avengerThx a lot, I really thought i will get no answer to my silly queston. ;P Though is there a method that will allow me to read no matter what line of the text file, without being obliged to read all the file? I mean like positioning the "cursor" to a certain position and read from there? What I mean is if I have a bigger file as: -2363824.109 (x position) 4870666.240 (y position) 2.18 (speed) 4566888.0035 (z position) 58.6544 (something) 269.2 (something) And i if i want to retrieve for example the 3rd line. Is there a simple solution? THX
-
Thx a lot, I really thought i will get no answer to my silly queston. ;P Though is there a method that will allow me to read no matter what line of the text file, without being obliged to read all the file? I mean like positioning the "cursor" to a certain position and read from there? What I mean is if I have a bigger file as: -2363824.109 (x position) 4870666.240 (y position) 2.18 (speed) 4566888.0035 (z position) 58.6544 (something) 269.2 (something) And i if i want to retrieve for example the 3rd line. Is there a simple solution? THX
First of all tell me one thing: do you need answers in terms of the "<<" operator or you just want an elegent solution? There is much better and elegent solution if you dont use "<<" technique.:-D Do you want the alternate technique or some modifications using "<<":confused:? Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs :)
-
Thx a lot, I really thought i will get no answer to my silly queston. ;P Though is there a method that will allow me to read no matter what line of the text file, without being obliged to read all the file? I mean like positioning the "cursor" to a certain position and read from there? What I mean is if I have a bigger file as: -2363824.109 (x position) 4870666.240 (y position) 2.18 (speed) 4566888.0035 (z position) 58.6544 (something) 269.2 (something) And i if i want to retrieve for example the 3rd line. Is there a simple solution? THX
You could if each line were the same length, or each xpos/ypos/speed group was the same size. The
seekg()
method is used to position the file pointer for the next read operation.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
First of all tell me one thing: do you need answers in terms of the "<<" operator or you just want an elegent solution? There is much better and elegent solution if you dont use "<<" technique.:-D Do you want the alternate technique or some modifications using "<<":confused:? Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs :)
-
You could if each line were the same length, or each xpos/ypos/speed group was the same size. The
seekg()
method is used to position the file pointer for the next read operation.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
First thx for friday. Then what do u mean by the same length? Is space or tab considered as getting in the value's lenth? Or just by adding zeros at the end? I looked in a tutorial for the skeeg stuff and didn't understand well how it works. Could u breif me about this, please? THX
-
i used here "<< " just to visualise things better, but if u have a better idea say it to me please :) THX
See, the best way (that i feel) of reading and writing srtuctured data to a file is using the following functions: istream::read ostream::write Function prototypes are: read( const unsigned char* ptr, int n) Description: The read function reads n characters starting form the location pointed to by ptr. In other words, read function reads n bytes from the address pointed to by ptr. The write has similar prototype and similar description (but used to write data) The above functions work best for reading/writing classes and structures from/to files. **EXAMPLE:**I include all your data in a class, say "A"
class A { double x; double y; double speed; double z; int something1; char something2; };
Then i write all these this to a file using the code:main() { A objectA; //take input from the user cin>>objectA.x; cin>>objectA.y; cin>>objectA.z; //and so on ofstream outfile ("c:\\test.txt", ios::binary); outfile.write ((char*)objectA, sizeof(objectA)); _//this line writes the complete objectA(field by field) into the file test.txt. Remember, the file contents are now binary. So u may see many strange character when you open test.txt in a notepad._ }
Once the object has been written into a file, you can read it anytime using using the code:main() { A objectB; ifstream infile ("c:\\test.txt", ios::binary); infile.read ((char*)objectB, sizeof(objectB)); _//after reading from the file objectB now contains the complete info of one class._ //DISPLAY the read results cout< YOU CAN CALL WRITE AGAIN AND AGAIN TO APPEND MORE TAHN ONE RECORDS INTO THE FILE SIMILAR IS THE READ FUNCTION. WHEN U CALL READ AGAIN, IT WILL READ THE NEXT OBJECT WRITTEN INTO THE FILE. All this may appear a bit fuzzy and complicated to you, BUT TRUST ME, this is the best way to handle structured data in files in C++. Ask if u have any doubts;) * * * Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
-
First thx for friday. Then what do u mean by the same length? Is space or tab considered as getting in the value's lenth? Or just by adding zeros at the end? I looked in a tutorial for the skeeg stuff and didn't understand well how it works. Could u breif me about this, please? THX
dairiseky wrote: Is space or tab considered as getting in the value's lenth? Yes, just because the character is not printable does not make it any less of a character. The
seekg()
method goes to a certain position within the file. It knows nothing of line-length. So if you had a two-line file, with the first line being 5 characters and the second line being 15 characters,seekg(6)
would go to the start of the second line, but if the length of the first line ever changed, it wouldn't. Understand?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
dairiseky wrote: Is space or tab considered as getting in the value's lenth? Yes, just because the character is not printable does not make it any less of a character. The
seekg()
method goes to a certain position within the file. It knows nothing of line-length. So if you had a two-line file, with the first line being 5 characters and the second line being 15 characters,seekg(6)
would go to the start of the second line, but if the length of the first line ever changed, it wouldn't. Understand?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
dairiseky wrote: Is space or tab considered as getting in the value's lenth? Yes, just because the character is not printable does not make it any less of a character. The
seekg()
method goes to a certain position within the file. It knows nothing of line-length. So if you had a two-line file, with the first line being 5 characters and the second line being 15 characters,seekg(6)
would go to the start of the second line, but if the length of the first line ever changed, it wouldn't. Understand?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
See, the best way (that i feel) of reading and writing srtuctured data to a file is using the following functions: istream::read ostream::write Function prototypes are: read( const unsigned char* ptr, int n) Description: The read function reads n characters starting form the location pointed to by ptr. In other words, read function reads n bytes from the address pointed to by ptr. The write has similar prototype and similar description (but used to write data) The above functions work best for reading/writing classes and structures from/to files. **EXAMPLE:**I include all your data in a class, say "A"
class A { double x; double y; double speed; double z; int something1; char something2; };
Then i write all these this to a file using the code:main() { A objectA; //take input from the user cin>>objectA.x; cin>>objectA.y; cin>>objectA.z; //and so on ofstream outfile ("c:\\test.txt", ios::binary); outfile.write ((char*)objectA, sizeof(objectA)); _//this line writes the complete objectA(field by field) into the file test.txt. Remember, the file contents are now binary. So u may see many strange character when you open test.txt in a notepad._ }
Once the object has been written into a file, you can read it anytime using using the code:main() { A objectB; ifstream infile ("c:\\test.txt", ios::binary); infile.read ((char*)objectB, sizeof(objectB)); _//after reading from the file objectB now contains the complete info of one class._ //DISPLAY the read results cout< YOU CAN CALL WRITE AGAIN AND AGAIN TO APPEND MORE TAHN ONE RECORDS INTO THE FILE SIMILAR IS THE READ FUNCTION. WHEN U CALL READ AGAIN, IT WILL READ THE NEXT OBJECT WRITTEN INTO THE FILE. All this may appear a bit fuzzy and complicated to you, BUT TRUST ME, this is the best way to handle structured data in files in C++. Ask if u have any doubts;) * * * Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
-
Ok I like this idea. I will try to use it. Could i, in case i want more details contact u on ur private mail? THX
If u follow the above mentioned technique, u can display any field, any time. Also u dont have to mess with the seek stuff. Thats very unreliable. You can personally contact me at avenger_sb25@yahoo.com or at avenger_sb25@hotmail.com I daily check both these mails. If u want, u can add me to your Yahoo Messenger or MSN Messenger ...cya
Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
-
dairiseky wrote: Is space or tab considered as getting in the value's lenth? Yes, just because the character is not printable does not make it any less of a character. The
seekg()
method goes to a certain position within the file. It knows nothing of line-length. So if you had a two-line file, with the first line being 5 characters and the second line being 15 characters,seekg(6)
would go to the start of the second line, but if the length of the first line ever changed, it wouldn't. Understand?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
Is it possible to get the size of datas to be written to file and then use this (their size), to be able to retrieve the right data? THX
Yes. Wasn't that answered here?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)