Thread: Reading from files

  1. #1
    Registered User
    Join Date
    May 2004

    Reading from files

    Hi, I was wondering how to read from files in c++ in a way that lets me know if there are any problems. I need this because when I load somthing like a level file, if the file isn't exactly right I want it to know and stop instead of carrying on and doing somthing upredictable.

    I used to use sscanf in c like this.
    char buffer[100];
    int x, y;
    if (sscanf(buffer, "x: %d, y: %d", &x, &y) < 2)
    What is the best way to do this in c++? Thanks

  2. #2
    Registered User
    Join Date
    Aug 2005
    I would do it almost the same way
    int x, y;
    std::string buffer;
    std::ifstream in("file");
    while( std::getline(in,buffer) )
    if (sscanf(buffer.c_str(),"x: %d, y: %d", &x, &y) < 2)
        std::cout << "Woops\n";

  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    One of the simplest ways to do that is to save each field on a separate line and use ifstream::getline() to read one at a time, and if you read fewer lines than expected or any line contains something it shouldn't, you know you have an error on your hands. One better way to accomplish your goals would be to save all of your data in a struct and write the entire thing to a file. Then, when you intend to load it, you can insure that a) the file contains exactly the size of your struct and b) each data field contains valid information.

  4. #4
    Registered User
    Join Date
    Jun 2005
    If using istream's check the status using
    //  do a read operation from the_stream
    if (the_stream)  no_error_has_occurred();
    if (!the_stream)  an_error_has_occurred();
    Most of the reading operators (eg operator >>(), getline()) will set status of the stream so that the above tests work. Some of the member functions return the number of bytes actually read: if this number is different from what is expected (eg you expect more than one byte to be read, and none are) it can also be used by your program as a primitive indication of an error.

    I seem to recall reading somewhere that some of the operations on streams throw exceptions in some cases if an error is encountered, but that may just be my foggy early morning memory.

  5. #5
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    The stream class provides plenty of functions for checking for errors after opening.

    ifstream inFile;"text.txt", ios::in);
    {     while(!
            {      // read from file 
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Isn't that kind of like this?
    If it is, you should read the FAQ . . . .

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 12:55 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  4. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 10:43 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM