Visual C++ Developer Center >
Visual C++ Forums
>
Visual C++ General
>
how to rewind a file and read the file from the beginning again?
how to rewind a file and read the file from the beginning again?
- I use
ifstream abc;
....
abc.clear();
abc.seekg( ios::beg);
plz help!!!
Answers
Try this: abc.seekg( 0, ios::beg).
- Marked As Answer byNancy ShaoMSFT, ModeratorFriday, November 13, 2009 8:45 AM
- 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- Marked As Answer byNancy ShaoMSFT, ModeratorFriday, November 13, 2009 8:45 AM
All Replies
- really not work
- is it just paste it when I want rewind everytime?
Try this: abc.seekg( 0, ios::beg).
- Marked As Answer byNancy ShaoMSFT, ModeratorFriday, November 13, 2009 8:45 AM
- 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- Marked As Answer byNancy ShaoMSFT, ModeratorFriday, November 13, 2009 8:45 AM
- 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.


