infile.get(x)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • las

    infile.get(x)

    I'm having a wee problem with the get method, here is my code :
    ifstream infile;
    char x;
    infile.open("te mp.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.


  • Rob Williscroft

    #2
    Re: infile.get(x)

    las wrote in news:I1NVa.1674 7$Hb.280412@new s4.e.nsc.no:
    [color=blue]
    > I'm having a wee problem with the get method, here is my code :
    > ifstream infile;
    > char x;
    > infile.open("te mp.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 ?
    >[/color]

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

    Comment

    • Spectre

      #3
      Re: infile.get(x)

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

      #include<iostre am>
      #include<fstrea m>
      using namespace std;

      int main()
      {
      ifstream infile;
      char x;
      infile.open("te mp.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" <laasunde@onlin e.no> wrote in message
      news:I1NVa.1674 7$Hb.280412@new s4.e.nsc.no...[color=blue]
      > I'm having a wee problem with the get method, here is my code :
      > ifstream infile;
      > char x;
      > infile.open("te mp.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.
      >
      >[/color]


      Comment

      • Karl Heinz Buchegger

        #4
        Re: infile.get(x)



        Spectre wrote:[color=blue]
        >
        > upon executing your code i got
        > B E E R[/color]

        But that's only your system :-)
        Other systems may behave differently.
        [color=blue]
        > in fact i think thats a sweet idea.....
        >[/color]

        [color=blue]
        > #include<iostre am>
        > #include<fstrea m>
        > using namespace std;
        >
        > int main()
        > {
        > ifstream infile;
        > char x;
        > infile.open("te mp.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';
        > }
        >[/color]

        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
        kbuchegg@gascad .at

        Comment

        • Dietmar Kuehl

          #5
          Re: infile.get(x)

          "las" <laasunde@onlin e.no> wrote:[color=blue]
          > while( !infile.eof() )
          > {
          > infile.get(x); //reads in characters
          > x=toupper(x); //converts characters to upper case
          > cout << x << '\t';
          > }[/color]

          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(st atic_cast<unsig ned char>(x)) << '\t';
          --
          <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
          Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.co m/>

          Comment

          • Kevin Goodsell

            #6
            Re: infile.get(x)

            tom_usenet wrote:
            [color=blue]
            > ...can only take the values EOF (-1)...[/color]

            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

            Comment

            • Greg Comeau

              #7
              Re: infile.get(x)

              In article <3f2817e6@shkne ws01>,
              Kevin Goodsell <usenet1.spamfr ee.fusion@never box.com> wrote:[color=blue]
              >tom_usenet wrote:[color=green]
              >> ...can only take the values EOF (-1)...[/color]
              >
              >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.[/color]

              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?

              Comment

              • Kevin Goodsell

                #8
                Re: infile.get(x)

                las wrote:
                [color=blue]
                > I'm having a wee problem with the get method, here is my code :
                > ifstream infile;
                > char x;
                > infile.open("te mp.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.
                >
                >[/color]

                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

                Comment

                Working...