CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Posts
    4

    filestream to read and write integer numbers

    Hi,

    I wrote a small program, which uses filestream to read and write integer numbers. The segment of code is as following:

    int i, j;
    while (!from.eof()) { // from is the input file
    from >> i >> j;
    std::cout << i <<j;
    }

    The input file includes int numbers 1 2 3 4. I expected that the output is 1 2 3 4. But it turns out to be 1 2 3 4 3 4.
    I can't figure out why it produces wrong result. Can someone give some ideas?

    Thanks!

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,723

    Re: filestream to read and write integer numbers

    Because eof() does not become true until you actually try reading past
    the EOF (not after the last read).

    Normally, you do not even check for eof() ...

    Code:
    while (from >> i >> j)
    {
       std::cout << i <<j;
    }

  3. #3
    Join Date
    Mar 2011
    Posts
    4

    Re: filestream to read and write integer numbers

    Hi Philip,

    It works. Thanks!

    Jianhttp://www.codeguru.com/forum/newreply.php?do=newreply&noquote=1&p=2020317

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured