this code display text file not correctly

Discussion in 'C++' started by chat, Jan 5, 2007.

  1. chat

    chat Guest

    Hi,

    I write program for writing and reading text file like this

    #include <iostream>
    #include <fstream>
    using namespace std;
    int main()
    {
    ofstream outfile("test.txt");
    for(int i=0;i<5;i++) outfile<<i<<endl;
    outfile.close();

    ifstream infile("test.txt");
    int a;
    while(infile) {
    infile>>a;
    cout<<a<<endl;

    }
    infile.close(); return 0;
    }

    The result is this
    0
    1
    2
    3
    4
    4

    Can anybody tell me what is the mistake in my program. It should show 0
    1 2 3 4 (only one 4).
    Why does it show digit 4 two times? (this code was compile with vc++ 6)
    Thank you in advance.
    chat watchara
    chat, Jan 5, 2007
    #1
    1. Advertising

  2. chat

    Andre Kostur Guest

    "chat" <> wrote in news:1167970544.821629.257510@
    38g2000cwa.googlegroups.com:

    > Hi,
    >
    > I write program for writing and reading text file like this
    >
    > #include <iostream>
    > #include <fstream>
    > using namespace std;
    > int main()
    > {
    > ofstream outfile("test.txt");
    > for(int i=0;i<5;i++) outfile<<i<<endl;
    > outfile.close();
    >
    > ifstream infile("test.txt");
    > int a;
    > while(infile) {
    > infile>>a;


    You don't check to see if this extraction succeeded. If it fails...

    > cout<<a<<endl;


    ....This line displays whatever a contained the last time around.

    >
    > }
    > infile.close(); return 0;
    > }
    >
    > The result is this
    > 0
    > 1
    > 2
    > 3
    > 4
    > 4
    >
    > Can anybody tell me what is the mistake in my program. It should show 0
    > 1 2 3 4 (only one 4).
    > Why does it show digit 4 two times? (this code was compile with vc++ 6)
    > Thank you in advance.
    > chat watchara
    >
    >
    Andre Kostur, Jan 5, 2007
    #2
    1. Advertising

  3. chat

    chat Guest

    How to correct this program in order to show result correctly?
    thank you very much.
    chat watchara
    chat, Jan 5, 2007
    #3
  4. chat

    David Harmon Guest

    On 4 Jan 2007 20:19:09 -0800 in comp.lang.c++, "chat"
    <> wrote,
    >How to correct this program in order to show result correctly?


    while(infile >> a)
    David Harmon, Jan 5, 2007
    #4
  5. chat

    chat Guest

    David Harmon à¢Õ¹:

    > while(infile >> a)


    You are cool. Your suggestion work well. thank you very much.
    But I still want to know why my program does not work?

    chat watchara
    chat, Jan 5, 2007
    #5
  6. chat

    chat Guest


    > David Harmon à¢Õ¹:
    > > while(infile >> a)


    How do you know which variable (infile or a) will used for condition
    entering into while loop.
    chat, Jan 5, 2007
    #6
  7. chat

    Sumit Rajan Guest

    Sumit Rajan, Jan 5, 2007
    #7
  8. chat

    chat Guest

    chat, Jan 5, 2007
    #8
  9. chat

    Jim Langston Guest

    "chat" <> wrote in message
    news:...

    > David Harmon à¢Õ¹:
    > > while(infile >> a)


    How do you know which variable (infile or a) will used for condition
    entering into while loop.

    intfile >> a
    returns a stream reference (istream I think, maybe something else) which has
    a bool override so can be used as a bool in an if statement which returns
    false if the stream is in a bad state. It will be in a bad state if some
    conditions occur, such as trying to read "x" into an int, or hitting the end
    of file.
    Jim Langston, Jan 5, 2007
    #9
    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. =?Utf-8?B?bGV0dWNlIGRhbmNl?=

    why does Response.ContentType = "text/plain"; not work correctly?

    =?Utf-8?B?bGV0dWNlIGRhbmNl?=, Oct 26, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    1,146
    =?Utf-8?B?bGV0dWNlIGRhbmNl?=
    Oct 26, 2006
  2. Marek Kurowski
    Replies:
    1
    Views:
    990
    Mark A. Odell
    Aug 18, 2004
  3. NJP
    Replies:
    1
    Views:
    688
    Kevin
    Mar 5, 2007
  4. cp
    Replies:
    3
    Views:
    206
    Jason
    Dec 24, 2004
  5. sed_y
    Replies:
    0
    Views:
    1,011
    sed_y
    Feb 15, 2012
Loading...

Share This Page