Help with getline() and file reading

This is a discussion on Help with getline() and file reading within the C++ Programming forums, part of the General Programming Boards category; I should point out first that I'm very new to C++: I can use functions cin, cout, getline and a ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    13

    Help with getline() and file reading

    I should point out first that I'm very new to C++: I can use functions cin, cout, getline and a few from the library string.h.

    I am attempting to use ifstream to pull in text line by line from a txt file, which works.

    However, when the file reader reaches the end of the file, I want it to start again from the top. This it won't do.

    here is the code I'm using:
    Code:
    ifstream txtfile("Z.txt");  //access the txt file.
    
    //here goes all my program code...
    
     if ( txtfile.eof() ){        //when reader has reached the end of the txt file.
                    txtfile.seekg(0,ios::beg);        //reset getline() internal pointer.
            }
    The seekg() function was suggested by a friend. I understand that it does something to the same pointer that getline() uses but I'm hazy on the details.
    Anyway, it doesn't work.


    Can anybody help me?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You have to clear the stream flags after reading through the file once. Use the clear() function to do that, otherwise it still thinks it's done even though the file pointer is back at the beginning.

    >> //here goes all my program code...
    That can't be all your code. There might be other issues depending on what the rest of your code looks like.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    There was a lot more code, but it was all manipulating strings and wasn't really relevent.

    But the clear() function works perfectly, thanks very much!

    Do you know of anywhere I could go to find out about stream flags and iostream and so forth? Not necessarily a tutorial, I just want to know how they work. A friend said they weren't functions, just weird variables, which confuses and interests me...
    Last edited by evilkillerfiggi; 10-15-2005 at 05:14 AM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Do you know of anywhere I could go to find out about stream flags and iostream and so forth? Not necessarily a tutorial, I just want to know how they work. A friend said they weren't functions, just weird variables, which confuses and interests me...
    They're called "objects", which involves the subject of "classes". Usually, you learn about pointers, references, and functions before tackling classes, and even then you might not explore the internals of an ifstream object until later.

    In short, an object is sort of like a variable, but like an array, it has several values associated with it. An object can also contain functions within it that can be called only using the object. For example, you might have something like this:
    Code:
    my_object
    -------------
    age = 20;
    name = "Fred";
    
    void display()
    {
          cout<<"age: "<<age;
          cout<<"name: "<<name;
    }
    ifstream objects have "flag" values that are set when an error occurs while reading input. Reaching eof is classified as an error, so when that happens the eof error flag is set in your ifstream object, which prevents reading any further input.
    Last edited by 7stud; 10-15-2005 at 12:27 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    >> //here goes all my program code...
    That can't be all your code.
    I think evilkillerfiggi meant that "This is where the rest of my code is - it's excluded from here", not "The rest of my code follows in this post."
    dwk

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    Yeah, dwks is right, there was a lot more code...
    But thanks very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from File
    By Bnchs in forum C Programming
    Replies: 8
    Last Post: 12-09-2007, 02:17 PM
  2. Reading in an array of text from a file?
    By suzakugaiden in forum C++ Programming
    Replies: 6
    Last Post: 01-04-2006, 02:17 PM
  3. Reading in data from a file
    By neandrake in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 08:04 PM
  4. Reading multiple lines from a file
    By cpluspluser in forum C++ Programming
    Replies: 4
    Last Post: 05-24-2003, 08:30 PM
  5. file reading
    By yougowego in forum C++ Programming
    Replies: 1
    Last Post: 10-28-2001, 03:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21