By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,514 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,514 IT Pros & Developers. It's quick & easy.

Reset ifstream peek value?

P: n/a
Dear All,

Is it possible to reset the value of xxx.peek() after it reachs EOF ?

Regards

HP
Nov 10 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hamburgpear wrote:
Dear All,

Is it possible to reset the value of xxx.peek() after it reachs EOF ?

Regards

HP


It's possible to reset the stream, is that what you mean? Use
xxx.clear() followed by xxx.seekp().

Resetting a function (which is what you said) doesn't make a lot of sense.

john
Nov 10 '05 #2

P: n/a
Thanks John,

Actually I have the following problems when I using ifstream.

I tried to count the total number of data from a txt file using while loop.
The terminating condition is (xxx.peek() != EOF).
Then, I allocate appropiate size to an array for data storage.
When I try to start a new loop to read data from the txt file, it will not
work becasue the return value of xxx.peek() is always equal to -1. It
doesn't work even I use a for-loop to read data with the same object. It
seems that the object "xxx" is still at the position of EOF. Am I right?

So, how to fix this problem? Now, I need to use another object for reading
and storing data. does it make sense to do it this way?

Thanks

HP

"John Harrison" <jo*************@hotmail.com> wrote in message
news:Jf*******************@newsfe7-gui.ntli.net...
Hamburgpear wrote:
Dear All,

Is it possible to reset the value of xxx.peek() after it reachs EOF ?

Regards

HP


It's possible to reset the stream, is that what you mean? Use xxx.clear()
followed by xxx.seekp().

Resetting a function (which is what you said) doesn't make a lot of sense.

john

Nov 10 '05 #3

P: n/a
Hamburgpear wrote:

Thanks John,

Actually I have the following problems when I using ifstream.

I tried to count the total number of data from a txt file using while loop.
The terminating condition is (xxx.peek() != EOF).
Then, I allocate appropiate size to an array for data storage.
When I try to start a new loop to read data from the txt file, it will not
work becasue the return value of xxx.peek() is always equal to -1. It
doesn't work even I use a for-loop to read data with the same object. It
seems that the object "xxx" is still at the position of EOF. Am I right?

So, how to fix this problem?


As John already said:
Your stream object has gone into an eof state. Once it does that it
refuses to work any more (by design). You need to clear that state
and the stream object continues working. Use the streams member function
clear() for that.
--
Karl Heinz Buchegger
kb******@gascad.at
Nov 10 '05 #4

P: n/a
Hamburgpear wrote:
Thanks John,

Actually I have the following problems when I using ifstream.

I tried to count the total number of data from a txt file using while loop.
The terminating condition is (xxx.peek() != EOF).
Then, I allocate appropiate size to an array for data storage.
When I try to start a new loop to read data from the txt file, it will not
work becasue the return value of xxx.peek() is always equal to -1. It
doesn't work even I use a for-loop to read data with the same object. It
seems that the object "xxx" is still at the position of EOF. Am I right?

So, how to fix this problem? Now, I need to use another object for reading
and storing data. does it make sense to do it this way?

Thanks

HP


So you want to go back to the beginning after hitting EOF.

xxx.clear();
xxx.seekp(0);

john
Nov 10 '05 #5

P: n/a

Hamburgpear wrote in message <43********@newsgate.hku.hk>...
Thanks John,

Actually I have the following problems when I using ifstream.

I tried to count the total number of data from a txt file using while loop.
The terminating condition is (xxx.peek() != EOF).
Then, I allocate appropiate size to an array for data storage.
When I try to start a new loop to read data from the txt file, it will not
work becasue the return value of xxx.peek() is always equal to -1. It
doesn't work even I use a for-loop to read data with the same object. It
seems that the object "xxx" is still at the position of EOF. Am I right?

So, how to fix this problem? Now, I need to use another object for reading
and storing data. does it make sense to do it this way?

Thanks
HP


Please don't top-post.
You don't need to hit the EOF to get the size of the file:

std::ifstream Ping("BR4.png", std::ios_base::binary );
if(!Ping){
std::cout<<"\n ifstream FAILED"<<std::endl;
}
std::cout<<" ifstream Ping.tellg() = "<<Ping.tellg()<<std::endl;
Ping.seekg(0, std::ios::end);
std::cout<<" Ping.seekg(0, ios::end) Ping.tellg() =
"<<Ping.tellg()<<std::endl;
Ping.seekg(0, std::ios::beg);
std::cout<<" Ping.seekg(0, ios::beg) Ping.tellg() =
"<<Ping.tellg()<<std::endl;
Ping.close();

// --- output ---
// ifstream Ping.tellg() = 0
// Ping.seekg(0, ios::end) Ping.tellg() = 4499
// Ping.seekg(0, ios::beg) Ping.tellg() = 0

Did that help?
--
Bob R
POVrookie
Nov 11 '05 #6

P: n/a

John Harrison wrote in message <9P*****************@newsfe2-win.ntli.net>...
Hamburgpear wrote:
Thanks John,

Actually I have the following problems when I using ifstream. [snip] So, how to fix this problem? Now, I need to use another object for reading and storing data. does it make sense to do it this way?
Thanks

HP

So you want to go back to the beginning after hitting EOF.

xxx.clear();
xxx.seekp(0);

john


<nit>
My (old) docs have 'seekp()' as an ostream operation. Did you mean 'seekg()'?
The OP stated 'ifstream'.

--
Bob R
POVrookie
Nov 11 '05 #7

P: n/a
>

<nit>
My (old) docs have 'seekp()' as an ostream operation. Did you mean 'seekg()'?
The OP stated 'ifstream'.


Yes I did. Sorry for the confusion.

john
Nov 11 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.