Problem with input streams

Discussion in 'C++' started by nadz, Oct 27, 2003.

  1. nadz

    nadz Guest

    just looking at this main

    int main()
    {
    ifstream fin("text.txt");
    doStuff(fin); //processes the data from text.txt
    fin.close();
    fin.open("text2.txt");
    doStuff(fin); //processes the data from text2.txt
    return 0;
    }

    This isn't the actual text from my code but this is basically what is going
    on. It compiles fine with no problems. Then when I run it. The text from
    text.txt gets processed with no problems. Then nothing happens with
    text2.txt. its compiled on g++ I don't know if it s a compiler thing or
    not. Are there flags that I have to change or something to reset fin? I
    thought all I had to do was close the old file and open a new one. Any help
    would be great on this matter. Also... along the same lines. What do I
    have to add in the code below in order to clear the stream and reset it so
    that I can read from the same file from the beginning again.

    int main()
    {
    ifstream fin("text.txt");
    doStuff(fin); //processes the data from text.txt

    //WHAT DO I PUT HERE

    doOtherStuff(fin); //processes the data from text.txt in a different way
    return 0;
    }

    Thanks in advance,
    Justin
    nadz, Oct 27, 2003
    #1
    1. Advertising

  2. nadz escribió:

    > int main()
    > {
    > ifstream fin("text.txt");
    > doStuff(fin); //processes the data from text.txt


    Insert
    fin.clear ();
    here.

    > fin.close();
    > fin.open("text2.txt");
    > doStuff(fin); //processes the data from text2.txt
    > return 0;
    > }


    Regards.
    =?iso-8859-1?Q?Juli=E1n?= Albo, Oct 27, 2003
    #2
    1. Advertising

  3. nadz

    Rolf Magnus Guest

    nadz wrote:

    > just looking at this main
    >
    > int main()
    > {
    > ifstream fin("text.txt");
    > doStuff(fin); //processes the data from text.txt
    > fin.close();
    > fin.open("text2.txt");
    > doStuff(fin); //processes the data from text2.txt
    > return 0;
    > }
    >
    > This isn't the actual text from my code but this is basically what is
    > going on. It compiles fine with no problems. Then when I run it.
    > The text from text.txt gets processed with no problems. Then nothing
    > happens with text2.txt. its compiled on g++ I don't know if it s a
    > compiler thing or not. Are there flags that I have to change or
    > something to reset fin?


    Exactly. The eof flag is still set from the last read operation in
    text.txt. As Julián Albo already suggested, you have to add fin.clear()
    to reset that flag (or any other flags that might stop the stream from
    working).

    > I thought all I had to do was close the old file and open a new one.


    Nope.

    > Any help would be great on this matter. Also... along the same lines.
    > What do I have to add in the code below in order to clear the stream
    > and reset it so that I can read from the same file from the beginning
    > again.
    >
    > int main()
    > {
    > ifstream fin("text.txt");
    > doStuff(fin); //processes the data from text.txt
    >
    > //WHAT DO I PUT HERE


    fin.seekg(0);
    fin.clear();


    >
    > doOtherStuff(fin); //processes the data from text.txt in a
    > different way
    > return 0;
    > }
    >
    > Thanks in advance,
    > Justin
    Rolf Magnus, Oct 27, 2003
    #3
  4. nadz

    Guest

    Justin wrote in message news:...
    []
    > int main()
    > {
    > ifstream fin("text.txt");
    > doStuff(fin); //processes the data from text.txt
    > fin.close();
    > fin.open("text2.txt");
    > doStuff(fin); //processes the data from text2.txt
    > return 0;
    > }
    >

    []
    > not. Are there flags that I have to change or something to reset fin? I
    > thought all I had to do was close the old file and open a new one. Any

    help
    > would be great on this matter. Also... along the same lines. What do I


    I'm no guru on streams so won't give you an elegant answer. But to just
    make your code 'work' you could try using nested blocks to 'refresh' fin
    each time you use it, thus:

    int main()
    {
    {
    ifstream fin("text.txt");
    doStuff(fin);
    }
    {
    ifstream fin("text2.txt"); // or s/text2/text
    doStuff(fin); // or s/Stuff/OtherStuff
    }
    }

    On second thought, the approach actually has a certain elegance
    because now you're treating each file EXACTLY the same. It can also
    solve your second problem (see comments).

    Hope that helps,
    Fazl

    > int main()
    > {
    > ifstream fin("text.txt");
    > doStuff(fin); //processes the data from text.txt
    >
    > //WHAT DO I PUT HERE
    >
    > doOtherStuff(fin); //processes the data from text.txt in a different

    way
    > return 0;
    > }

    []
    , Nov 9, 2003
    #4
    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. Pawel Banys
    Replies:
    1
    Views:
    485
    Joe Smith
    Jul 8, 2004
  2. Replies:
    0
    Views:
    375
  3. Matthew David Hills

    input streams, setw(), and strings vs. char*?

    Matthew David Hills, Sep 5, 2003, in forum: C++
    Replies:
    1
    Views:
    561
    John Harrison
    Sep 5, 2003
  4. Generic Usenet Account
    Replies:
    7
    Views:
    931
    Nimmi Srivastav
    Nov 2, 2005
  5. Peng Yu

    How to take two input streams?

    Peng Yu, Jun 1, 2010, in forum: Perl Misc
    Replies:
    10
    Views:
    244
    RedGrittyBrick
    Jun 2, 2010
Loading...

Share This Page