RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Weird problem

MarciXs

Sorceror
Weird problem

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.
 

daat99

Moderator
Staff member
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.
 

Sep102

Page
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;
    }
}
 

MarciXs

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

MarciXs

Sorceror
Sep102;714260 said:
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;
    }
}

I just tried that clear. All I can say is THANK YOU VERY MUCH
 
Top