infile.get(x)

Discussion in 'C++' started by las, Jul 30, 2003.

  1. las

    las Guest

    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.
     
    las, Jul 30, 2003
    #1
    1. Advertising

  2. las wrote in news:I1NVa.16747$:

    > 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/
     
    Rob Williscroft, Jul 30, 2003
    #2
    1. Advertising

  3. las

    Spectre Guest

    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" <> wrote in message
    news:I1NVa.16747$...
    > 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.
    >
    >
     
    Spectre, Jul 30, 2003
    #3
  4. 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
     
    Karl Heinz Buchegger, Jul 30, 2003
    #4
  5. "las" <> 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';
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
     
    Dietmar Kuehl, Jul 30, 2003
    #5
  6. 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
     
    Kevin Goodsell, Jul 30, 2003
    #6
  7. 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
     
    Kevin Goodsell, Jul 30, 2003
    #7
  8. las

    Greg Comeau Guest

    In article <3f2817e6@shknews01>,
    Kevin Goodsell <> 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?
     
    Greg Comeau, Jul 30, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Vinokur

    infile.rdbuf() for empty file

    Alex Vinokur, Feb 2, 2005, in forum: C++
    Replies:
    5
    Views:
    811
    Dietmar Kuehl
    Feb 3, 2005
  2. questioner
    Replies:
    1
    Views:
    373
    A. Lloyd Flanagan
    May 14, 2004
  3. waltbrad

    "inFile" object cannot read EOF

    waltbrad, Jun 7, 2007, in forum: C++
    Replies:
    15
    Views:
    627
    Jerry Coffin
    Jun 11, 2007
  4. Replies:
    2
    Views:
    378
    Dennis Lee Bieber
    Jun 10, 2007
  5. Balldrew J. Bag
    Replies:
    1
    Views:
    132
    Michael Budash
    Jul 26, 2003
Loading...

Share This Page