Thread: How do I know if I am at the end of a file (and other FILEIO Qs?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    This is what you need to do:
    1)create a buffer to store one line of text from the file
    2)open the file for reading
    3) test the stream to make sure it is open
    4) read one line from the file
    5) test for end of file
    6) if end of file, quit
    7) else increment line count, goto 4

    Hints:
    1) use: char line[256]; for the buffer
    2) use: int count=0; to hold the line count
    3) use: if(in) to test the stream for open state
    4) use: while( in.getline(line, 256, '\n')) to read from the file and test for end of file in one line of code;
    5) use: ++count; to increment the count

    I gave you a lot of big hints because you are very new at C++. See if you can put this "old school" code together.

    edit:
    you can get the users input like this
    Code:
    char filename[MAX_PATH];  // need a place to put the filename
    
    std::cout << "filename: ";
    std::cin >> filename;
    
    // use filename to open the stream
    Rog
    Last edited by Rog; 03-07-2005 at 04:27 AM.

Popular pages Recent additions subscribe to a feed