infile.get(x)

L

las

I'm having a wee problem with the get method, here is my code :
ifstream infile;
char x;
infile.open("temp.txt");
if( !infile.good() )
{
cout << "Error opening file" << endl;
system("PAUSE");
exit(1);
}

while( !infile.eof() )
{
infile.get(x); //reads in characters
x=toupper(x); //converts characters to upper case
cout << x << '\t';
}

The file temp.txt contains one word - "beer". However when I run the above
code I get BEERR (notice the two R's at the end). What gives ?

Thanks for any help.
 
R

Rob Williscroft

las wrote in
I'm having a wee problem with the get method, here is my code :
ifstream infile;
char x;
infile.open("temp.txt");
if( !infile.good() )
{
cout << "Error opening file" << endl;
system("PAUSE");
exit(1);
}

while( !infile.eof() )
{
infile.get(x); //reads in characters
x=toupper(x); //converts characters to upper case
cout << x << '\t';
}

The file temp.txt contains one word - "beer". However when I run the
above code I get BEERR (notice the two R's at the end). What gives ?

you mean: B E E R R

eof() doesn't get set until you try to read a char beyond the
EOF. Unfortunatly eof() is an after the event error not a before the
event warning.

try:

while ( infile.get( x ) )
{
cout << toupper( x ) << "\t";
}

HTH

Rob.
 
S

Spectre

upon executing your code i got
B E E R
in fact i think thats a sweet idea.....

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
ifstream infile;
char x;
infile.open("temp.txt");
if( !infile.good() )
{
cout << "Error opening file" << endl;
system("PAUSE");
exit(1);
}

while( !infile.eof() )
{
infile.get(x); //reads in characters
x=toupper(x); //converts characters to upper case
cout << x << '\t';
}

return 0;
}
 
K

Karl Heinz Buchegger

Spectre said:
upon executing your code i got
B E E R

But that's only your system :)
Other systems may behave differently.
in fact i think thats a sweet idea.....


#include<iostream>
#include<fstream>
using namespace std;

int main()
{
ifstream infile;
char x;
infile.open("temp.txt");
if( !infile.good() )
{
cout << "Error opening file" << endl;
system("PAUSE");
exit(1);
}

while( !infile.eof() )
{
infile.get(x); //reads in characters
x=toupper(x); //converts characters to upper case
cout << x << '\t';
}

eof gets true only *after* you have *tried* and *failed*
to read from the stream.

the program enters the while loop and processes 'b' 'e' and 'e'.
The next time the loop is entered the program reads the next
character: 'r'. The character is converted to upper case and
sent to cout. eof is tested again and returns false! Remember:
you need to try and fail to read from the stream.
Thus the loop is entered again and an attempt to read from
infile is made. But this time the attempt fails, since there
is nothing more in the stream that could be read. Yet your
loop body continues as if nothing has happend and converts
and outputs x. Only after that, eof will return true.
End effect: You looped one to many through the loop and
try to process a character, whose read operation from infile
has failed.

Whenever you see a loop

while( somestream.eof() )
{
read from somestream
process
}

then almost always, this is wrong. Most programmer think
this way:

as long as I have not reached the end of the file
{
read frm the file
process what has been read
}

This may work in other programming lanquages, but not in C++.
In C++ one, correct, way to think is:

as long as I can read from the file
{
process what has been read
}

why did the read fail? Was it because eof?
If yes, then everything is OK. The stream was
read completely.


or in code:

while( infile.get( x ) )
{
x = toupper( x );
cout << x << '\t';
}

if( !infile.eof() )
{
cout << "Read operation failed for unknown reasons\n";
...
}
 
D

Dietmar Kuehl

las said:
while( !infile.eof() )
{
infile.get(x); //reads in characters
x=toupper(x); //converts characters to upper case
cout << x << '\t';
}

You shall check whether the read from the file was successful: the EOF flag
is guaranteed to be set only *after* you attempted to read past the end of
file. It does not make sense to set it prior to the extraction because it
is unknown how many characters you will expect.

BTW, note that 'toupper()' takes only values representable as 'unsigned
char' but the type 'char' may be signed. You probably want to do something
like this:

while (infile.get(x))
std::cout << std::toupper(static_cast<unsigned char>(x)) << '\t';
 
K

Kevin Goodsell

tom_usenet said:
...can only take the values EOF (-1)...

Unless I'm mistaken, EOF is only guaranteed to be a negative int value,
so it may or may not be -1. At least I am quite sure this is the case in
C, and I can't find anything in the C++ standard that changes it.

-Kevin
 
K

Kevin Goodsell

las said:
I'm having a wee problem with the get method, here is my code :
ifstream infile;
char x;
infile.open("temp.txt");
if( !infile.good() )
{
cout << "Error opening file" << endl;
system("PAUSE");
exit(1);
}

while( !infile.eof() )
{
infile.get(x); //reads in characters
x=toupper(x); //converts characters to upper case
cout << x << '\t';
}

The file temp.txt contains one word - "beer". However when I run the above
code I get BEERR (notice the two R's at the end). What gives ?

Thanks for any help.

A few additional notes:

Please post complete code when you ask a question. We should be able to
copy, paste, and compile if we need to.

Errors should usually be written to std::cerr, not std::cout.

system("PAUSE") is not portable, and is probably only useful for
debugging anyway. There are probably other ways you could accomplish
what you want, that don't require adding an extra function call before
every exit() call (actually, using exit() isn't necessarily a good idea
anyway - throwing an exception that is caught in main() is probably better).

exit(1) is not portable. There are 3 return values for your program that
have defined meanings according to the standard. They are 0,
EXIT_SUCCESS, and EXIT_FAILURE. The first two have the same meaning, and
may or may not have the same value.

-Kevin
 
G

Greg Comeau

Unless I'm mistaken, EOF is only guaranteed to be a negative int value,
so it may or may not be -1. At least I am quite sure this is the case in
C, and I can't find anything in the C++ standard that changes it.

That's right (for both C and C++).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top