Problem with input streams

N

nadz

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
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

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

Rolf Magnus

nadz said:
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).
 
F

Faz

Justin wrote in message []
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;
}
[]
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top