Archived

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

ryanc

Basic IO: Reading a file

Recommended Posts

ryanc    136
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

do
{
     infile>>x;             //Statement A

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

}while(!infile.eof());
    
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
deadlinegrunt    123
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.


YAP-YFIO,
deadlinegrunt

Share this post


Link to post
Share on other sites
ryanc    136
I''ll try that. Thanks. Maybe I can figure it out.


RyanC (C++ Beginner)
---------------------------
Games are only as good as you make them...

Share this post


Link to post
Share on other sites