reading from file
-
How do I read from a file starting at a particular line. Suppose I want to start retrieving data from the third line but I don't know how much data is on the first two lines. Can I just read until I get two carriage returns? Something like: ifstream file(test.in); char test; for(int i=0; i<2; i++) { while(test!='\n') file >> test; } I know this code is most probably not right, I just put it in to help explain what it is I want I'm trying to accomplish. Can anyone tell me the right way to do it?
-
How do I read from a file starting at a particular line. Suppose I want to start retrieving data from the third line but I don't know how much data is on the first two lines. Can I just read until I get two carriage returns? Something like: ifstream file(test.in); char test; for(int i=0; i<2; i++) { while(test!='\n') file >> test; } I know this code is most probably not right, I just put it in to help explain what it is I want I'm trying to accomplish. Can anyone tell me the right way to do it?
-
How do I read from a file starting at a particular line. Suppose I want to start retrieving data from the third line but I don't know how much data is on the first two lines. Can I just read until I get two carriage returns? Something like: ifstream file(test.in); char test; for(int i=0; i<2; i++) { while(test!='\n') file >> test; } I know this code is most probably not right, I just put it in to help explain what it is I want I'm trying to accomplish. Can anyone tell me the right way to do it?
I have used that approach before. Probably not the best, but it works. Only you should take care when compiling under linux/unix...I don't think they support the same CRLF sequence I think it's just CR on those systems. "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
-
How do I read from a file starting at a particular line. Suppose I want to start retrieving data from the third line but I don't know how much data is on the first two lines. Can I just read until I get two carriage returns? Something like: ifstream file(test.in); char test; for(int i=0; i<2; i++) { while(test!='\n') file >> test; } I know this code is most probably not right, I just put it in to help explain what it is I want I'm trying to accomplish. Can anyone tell me the right way to do it?
As mentioned previously,
getline()
will prolly work well for you. But if you want to do it your way, you should test also for EOF, just in case there are less than two lines in the file. ;)_**Sometimes i only remember, The days when i was young
Nowadays no one remembers when they were young and stupid...**_
ADEMA, The Way You Like It
-
How do I read from a file starting at a particular line. Suppose I want to start retrieving data from the third line but I don't know how much data is on the first two lines. Can I just read until I get two carriage returns? Something like: ifstream file(test.in); char test; for(int i=0; i<2; i++) { while(test!='\n') file >> test; } I know this code is most probably not right, I just put it in to help explain what it is I want I'm trying to accomplish. Can anyone tell me the right way to do it?
If you don't mind using vanilla C, you can use
fgets()
to read one line at a time. /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com