Thread: how to reset getline?

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    24

    Question how to reset getline?

    hey,

    Does anybody know how to reset getline, that is, to make it read from the beginning of the file again?

    thanks
    Dmitry Kashlev

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    I don't know about fstreams, but if you're using FILE *'s then you can just rewind it by saying.
    Code:
    rewind (fp);
    edit:
    I found out how to do it with fstreams, just use seekg.
    Code:
    fp.seekg (0, ios::beg);
    Last edited by Crimpy; 08-10-2002 at 04:21 PM.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    24
    what is fp?
    What header does rewind() belong to?

    thanks
    Dmitry Kashlev

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    fp is the FILE pointer.
    Code:
    FILE *fp = fopen (FILENAME, "r");
    if (!fp){
      cerr<<"The file didn't open"<<endl;
      exit (EXIT_FAILURE);
    }
    rewind is in the cstdio header, seekg is in the fstream header.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    'getline()' wouldn't be an appropriate choice when working with file data, so your question is somewhat ambiguous, however...

    'getline()' and 'get()' are "destructive" reading processes.

    Both must read and remove at least one character from the stream. If not, the stream failbit() is set and you've got problems to deal with.

    rewind() and seekg(0, ios::beg) are both methods of repositioning the file pointer to the beginning of a file, just as Crimpy has mentioned.

    (seekp(0, ios::beg) does the same as seekg(). The 'p' and 'g' are put() and get() designators - istream and ostream, respectively).

    You might investigate these three functions: putback(), unget() and peek(). These don't apply to file data, but might be handy to have in your arsenal anyway.

    In case the answer to your question got "glossed over" here, since you refer specifically to files, use read(). This function is non-destructive to the file data.

    Also, when opening your file, make sure you don't use 'ios::trunc' unless you want to render this entire discussion moot.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  6. #6
    elad
    Guest
    >>, getline(), get(), peek(), putback(), read(), seekg() are all istream methods/operators, and as such are inhereted by ifstreams and subsequently, fstreams. getline() and get() and >> all remove data from the stream buffer, not from the file. reading a file with getline() or get() will not destroy the file. Writing to a file using something other than the app mode will "destroy" the file, however.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    elad,

    Good info. I was thinking more in terms of a standard input stream regarding getline() and get() vice file data, but your clarification is appreciated, by me anyway.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 11:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 02:59 PM