This topic is now archived and is closed to further replies.


Basic IO: Reading a file

Recommended Posts

I am slightly confused on reading a file. I know how to initialize and open a file for reading but I''m a little bit fuzzy on the actual reading part. Here''s an example code I got out of a book:
//This is where the statements that initialize go

     infile>>x;             //Statement A

     cout<<x<<end1;         //Statement B

I think Statement A tells the program to read the int on the first line on the file being read, and once finished, go to next line. Please explain this to me. I also have another question: How could I (if the line number was known) go to a specific line in the file being read and print the infomation after it for a certain amount of lines, with the information being read in a char format. Any tips are appreciated! RyanC (C++ Beginner) --------------------------- Games are only as good as you make them...

Share this post

Link to post
Share on other sites
Statement A will read in (from the current fpos) all non-whitspace items until it reaches whitespace or a terminating character in your example (end of line for example). Unless of course you change any of the ios flags, but I''m not gonna do the what if game right now...

For your second part, look up the tellg and getline member functions of ifstream. (Those are actually members of basic_streambuf I think, but inheritence and all, you should be able to find it with F1)

The statements above are not THE definitive answers/solutions. (Always more than one way to flip a bit) It''s late and I shouldn''t be here right now, but that will get you looking in the right place anyway.


Share this post

Link to post
Share on other sites