|
||
|
|
#1 (permalink) |
|
Forum Expert
Join Date: Oct 2003
Location: Birmingham, UK
Age: 20
Posts: 423
|
What could be the reason that If I have many ifstreams,ofstreams,fstreams. . After a while , It gets buggy , for example. I have for(int x= 0;x<100;x++){ instream.open("readme.txt",ios::in);
while(! in.eof()){ in.getline(somedata,1024); cout << somedata << endl; } --- problem usually is that the ifstream in this case instream can't do it more than once, what i mean is. It just does it one time and the rest of the loop it just prints out empty line. Does anyone knows why could this happen? what I wanted to know is , why could instream/outstream start to get buggy? I have many pointers which haven't been initialized, could be that the problem? I have : class myclass { char * one; char * two; char * pch; struct data[] { char rl[1024]; etc etc. }datas[10]; ifstream ones; fstream yuu; ofstream etc; int al; char * bb; char * cc; void darit(char * a,char * b , int c,int j,char * l); .....etc etc Thank you. |
|
|
|
|
|
#2 (permalink) |
|
RunUO Forum Moderator
|
Try to close the stream before you end the first iteration of the loop so when you enter the next iteration the stream will be closed and it'll open the file in the new stream again.
__________________
I always try to help
![]() Sometimes, I don't know how.... ![]() My Web Page Forum Rules ------------------------------------------------------------- Extensive OWLTR System | Token System | World Teleporters ------------------------------------------------------------- |
|
|
|
|
|
#3 (permalink) |
|
Forum Expert
|
Apparently (unbeknownst to me at least) you also need to call clear() on the stream after closing the file (at least when using the standard libraries with Visual Studio) to actually reset EOF on the stream.
No idea why close() doesn't do this automatically, but, it doesn't. Concerning this problem, however, I would recommend actually declaring the stream in the loop body, allowing it to be destructed and reconstructed each time rather than reusing the same one each time (since, efficiency-wise, it doesn't matter in th least since recreating and destructing the object each time is nothing compared to actually doing the file I/O itself, and I can't think of any other very compelling reason to reuse the stream object). So, Code:
for(int i = 0; i < 100; ++i)
{
ifstream ifs("File.ext");
string s;
while(!ifs.eof())
{
getline(ifs, s);
cout << s << endl;
}
}
Last edited by Sep102; 09-26-2007 at 05:48 PM. |
|
|
|
|
|
#4 (permalink) |
|
Forum Expert
Join Date: Oct 2003
Location: Birmingham, UK
Age: 20
Posts: 423
|
I dunno why but for some reason after my program got bigger and bigger ( I really need to rewrite it again...) when I had two streams opened at once it started to get buggy.
I just found out that if I had like: ifstream one and ifstream two; For example; one.open("data.txt",ios::in); while(!one.eof){ one >> data >> bytess; two.open(data,ios::in){ two >> bits >> kool; } two.close; } It started to bug after the first loop , but I am more than sure that it didn't that before I implented more functions with more pointers/streams. Anyways its solved now. Thanks a lot Sep102 , I will try that close() now. |
|
|
|
|
|
#5 (permalink) | |
|
Forum Expert
Join Date: Oct 2003
Location: Birmingham, UK
Age: 20
Posts: 423
|
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|