Microsoft Developer Network > Forenhomepage > Visual C++ General > how to rewind a file and read the file from the beginning again?
Stellen Sie eine FrageStellen Sie eine Frage
 

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

Antworten

  • Sonntag, 8. November 2009 09:44Viorel_MVPTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet

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

  • Sonntag, 8. November 2009 22:18WayneAKing TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    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

Alle Antworten