Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > infile.get(x)

Reply
Thread Tools

infile.get(x)

 
 
las
Guest
Posts: n/a
 
      07-30-2003
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.


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      07-30-2003
las wrote in news:I1NVa.16747$(E-Mail Removed):

> 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.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
 
Spectre
Guest
Posts: n/a
 
      07-30-2003
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;
}

"las" <(E-Mail Removed)> wrote in message
news:I1NVa.16747$(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      07-30-2003


Spectre wrote:
>
> 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";
...
}

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Dietmar Kuehl
Guest
Posts: n/a
 
      07-30-2003
"las" <(E-Mail Removed)> wrote:
> 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';
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      07-30-2003
tom_usenet wrote:

> ...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

 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      07-30-2003
las wrote:

> 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

 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      07-30-2003
In article <3f2817e6@shknews01>,
Kevin Goodsell <(E-Mail Removed)> wrote:
>tom_usenet wrote:
>> ...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.


That's right (for both C and C++).
--
Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments