Visual C++ Developer Center > Visual C++ Forums > Visual C++ General > how to rewind a file and read the file from the beginning again?
Ask a questionAsk a question
 

Answerhow to rewind a file and read the file from the beginning again?

Answers

  • Sunday, November 08, 2009 9:44 AMViorel_MVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Try this: abc.seekg( 0, ios::beg).

  • Sunday, November 08, 2009 10:18 PMWayneAKing Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Harry -

    Just a word of explanation regarding Viorel_'s suggestion:

    The suggested format is the correct way to seek to the beginning of a file:

    seekg(0, ios::beg);

    The allowed formats of a seekg/seekp are:

    seekg(pos);
    where pos is a position to seek to in the file

    seekg(offset, direction);
    where offset is number of chars from the position defined by "direction",
    which may be beg, cur, or end.

    Typically these constants will be implemented as an enum, with
    beg == 0
    cur == 1
    end == 2

    With such an implementation, using

    seekg(ios::beg);

    may actually work as intended, but only by blind luck as it is seen by
    the compiler as equal to seekg(0);

    However, an attempt to do

    seekg(ios::end);

    will not have the desired effect, as it's equivalent to seekg(2);

    The correct format to position to the end of a file is

    seekg(0, ios::end);

    Now since VC++ appears to use the values I gave above, making the
    change to the correct format given by Viorel_ may have no effect
    on the problem you're having. You need to be more specific, and if
    possible give an example of what's happening as well as how that
    differs from what you were expecting/wanting to happen.

    - Wayne

All Replies

  • Sunday, November 08, 2009 8:05 AM«_Superman_»MVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    This looks good.
    Does it not work?

    «_Superman_»
    Microsoft MVP (Visual C++)
  • Sunday, November 08, 2009 8:06 AMharry chan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    really not work
  • Sunday, November 08, 2009 8:07 AM«_Superman_»MVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Problem what ?
    «_Superman_»
    Microsoft MVP (Visual C++)
  • Sunday, November 08, 2009 8:08 AMharry chan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    is it just paste it when I want rewind everytime?
  • Sunday, November 08, 2009 8:10 AM«_Superman_»MVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I really don't understand you.
    I give up.

    «_Superman_»
    Microsoft MVP (Visual C++)
  • Sunday, November 08, 2009 9:44 AMViorel_MVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Try this: abc.seekg( 0, ios::beg).

  • Sunday, November 08, 2009 10:18 PMWayneAKing Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Harry -

    Just a word of explanation regarding Viorel_'s suggestion:

    The suggested format is the correct way to seek to the beginning of a file:

    seekg(0, ios::beg);

    The allowed formats of a seekg/seekp are:

    seekg(pos);
    where pos is a position to seek to in the file

    seekg(offset, direction);
    where offset is number of chars from the position defined by "direction",
    which may be beg, cur, or end.

    Typically these constants will be implemented as an enum, with
    beg == 0
    cur == 1
    end == 2

    With such an implementation, using

    seekg(ios::beg);

    may actually work as intended, but only by blind luck as it is seen by
    the compiler as equal to seekg(0);

    However, an attempt to do

    seekg(ios::end);

    will not have the desired effect, as it's equivalent to seekg(2);

    The correct format to position to the end of a file is

    seekg(0, ios::end);

    Now since VC++ appears to use the values I gave above, making the
    change to the correct format given by Viorel_ may have no effect
    on the problem you're having. You need to be more specific, and if
    possible give an example of what's happening as well as how that
    differs from what you were expecting/wanting to happen.

    - Wayne
  • Tuesday, November 10, 2009 8:01 AMNancy ShaoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Harry,

    Just addition information, please see istream& seekg function in MSDN with following link for more information:

    http://msdn.microsoft.com/en-us/library/aa277370(VS.60).aspx

    Best Regards,
    Nancy
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.