| View previous topic :: View next topic |
| Author |
Message |
nadz Guest
|
Posted: Mon Oct 27, 2003 8:04 pm Post subject: Problem with input streams |
|
|
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
|
|
| Back to top |
|
 |
Julián Albo Guest
|
Posted: Mon Oct 27, 2003 8:30 pm Post subject: Re: Problem with input streams |
|
|
nadz escribió:
| Quote: | int main()
{
ifstream fin("text.txt");
doStuff(fin); //processes the data from text.txt
|
Insert
fin.clear ();
here.
| Quote: | fin.close();
fin.open("text2.txt");
doStuff(fin); //processes the data from text2.txt
return 0;
}
|
Regards.
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Mon Oct 27, 2003 11:02 pm Post subject: Re: Problem with input streams |
|
|
nadz wrote:
| Quote: | 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).
| Quote: | I thought all I had to do was close the old file and open a new one.
|
Nope.
| Quote: | 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();
| Quote: |
doOtherStuff(fin); //processes the data from text.txt in a
different way
return 0;
}
Thanks in advance,
Justin
|
|
|
| Back to top |
|
 |
Faz@nowhere.com Guest
|
Posted: Sun Nov 09, 2003 4:26 pm Post subject: Re: Problem with input streams |
|
|
Justin wrote in message news:vpquig4r92bld9 (AT) corp (DOT) supernews.com...
[]
| Quote: | 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
| Quote: | 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;
}
[] |
|
|
| Back to top |
|
 |
|