473,395 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Reset ifstream peek value?

Dear All,

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

Regards

HP
Nov 10 '05 #1
7 5614
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
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
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
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

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

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
>

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alex Vinokur | last post by:
I have several questins concerning a program below. ------ foo.cpp : BEGIN ------ #include <cassert> #include <cstdlib> #include <iostream> #include <fstream> using namespace std; int...
1
by: Dan | last post by:
Hi, I'm having a problem with StreamReader.Peek(). Let's say I open a file and read it to end; then I'd want to move its stream pointer back to the file beginning: I can call BaseStream Seek method...
6
by: Dave | last post by:
In .Net 2003 if a line, read from a text file is larger than a size parameter, the ifstream getline(buff, sze) put the file pointer to the EOF, so next peek() returns EOF. I saw this problem...
1
by: James Lehman | last post by:
Hello. I want to write a program that reads AutoCAD shape (font) files. They are written with the convention that hexadecimal values have a leading zero and decimal values do not. All numbers...
3
by: omariqbalnaru | last post by:
Is there any way to identify end of line while reading data from an ifstream?
10
by: SpreadTooThin | last post by:
I just did a loop ifstream i(myfile, ios::binary); while (!i.eof()) { i.read(buff, 2); } Well it should have come out of the loop but it tried to do the read
1
by: Vadim Volevach | last post by:
Hi, // Under C++Builder 2007 (+ Dinkumware) #include <fstream> using namespace std; int main (int argc, char* argv) { // Create a DOS-Text file, that has only two lines: // - the first...
2
by: ultr | last post by:
Hello, I was looking for a solution on http://www.cplusplus.com/ and serched groups, but cannot find anything interesting. My program opens an input file stream: ifstream f( "in.txt" ); then...
2
by: Terry Reedy | last post by:
Luis Zarrabeitia wrote: Interesting observation. Iterators are intended for 'iterate through once and discard' usages. To zip a long sequence with several short sequences, either use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.