Skip to main content
gamedev.net
🔒 Locked

Basic IO: Reading a file

Started by ryanc Oct 29, 2000 at 12:23 AM 1 replies 850+ views
Original Post
ryanc
ryanc
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...
---------------------------<code>!ERR(0493): An error occured in cerebral cortex at location 0x00f230f8!: Buffer Overflow
deadlinegrunt
deadlinegrunt
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
~deadlinegrunt
ryanc
ryanc
I''ll try that. Thanks. Maybe I can figure it out.


RyanC (C++ Beginner)
---------------------------
Games are only as good as you make them...
---------------------------<code>!ERR(0493): An error occured in cerebral cortex at location 0x00f230f8!: Buffer Overflow

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.