Reputation: 220
As I have so much problem while dealing with the eof of a file, whenever I code with fstream
and the eof
appears I have to clear the stream in order to work with that stream. Although I have searched a lot about the eof
and I got the result that I should start using:
fstream file("Filename.txt",ios::in|ios::ate|ios::out);
char str[80];
while(file>>str)
{
//do the required stuff
}
//clear the stream and reuse it
file.clear();
file.seekp(0);
But I have also read about a function called peek()
which is also used for such purposes but I am a little confused in its working and I am not able to apply it in the code. So if anyone could guide me through this.
And I have also heard about a function called putback()
what's that??
Edit-1
fstream file("Filename.txt",ios::in|ios::ate|ios::out);
char str[80];
while(file>>str)
{
//do the required stuff
}
//clear the stream and reuse it
file.clear();
file.seekp(0);
//Now do the required writing operation after reading the whole file wherever is required
//I also want to perform writing operations and if this pattern seems most suitable for me
Upvotes: 2
Views: 6277
Reputation: 129374
Say you want to write a parser for C or C++ and your code does something like this:
char c = source.get();
switch(c)
{
...
case '<':
// May be < or <=
if (source.peek() == '=')
{
source.get();
return less_or_equal;
}
// Ok, not <= so:
return less;
...
}
[I ignored that it may be part of a template, a shift, or something else like that]
The need for putback()
is very little when you have peek()
, but it does allow code that "normally consumes" the character to put it back "if it got it wrong". Say you know that <=
is more common than <
, then you could do:
char c = source.get();
switch(c)
{
...
case '<':
// May be < or <=
c = source.get();
if (c == '=')
{
source.get();
return less_or_equal;
}
source.putback(c);
// Ok, not <= so:
return less;
...
}
because it only does putback
on the rare case [as per the assumed statistics above].
One can imagine cases where the common case is to get
and the rare case is mismatch, e.g. if we want to read a number:
int number = 0;
do
{
char c = input.get();
if (isdigit(c))
{
number *= 10;
number += c - '0';
}
else
{
input.putback(c);
}
while( isdgit(c) );
Since, most numbers have more than one digit in them, the more common case is that the first and the subsequent character is a digit, and the unusual case is that we need to call putback()
. [Of course, reading numbers "properly" will require a bit more stuff...]
Upvotes: 2
Reputation: 726619
But I have also read about a function called
peek()
which is also used for such purposes
peek()
was created for a different purpose - it's there to let your program process two characters at a time - essentially emulating ungetc()
functionality from the portion of the library based on the C standard library.
Using peek
to see if you are about to hit eof
is valid, but the approach that you show in the code, i.e. while(file>>str)
, is more idiomatic to C++ than the one based on peek
.
I have also heard about a function called
putback()
what's that?
The std::putback()
function lets you do the same thing as ungetc()
does for FILE*
streams.
Upvotes: 2