Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C/C++

C/C++ C/C++ Discussion

Reply
 
Thread Tools Display Modes
Old 09-25-2007, 02:48 AM   #1 (permalink)
Forum Expert
 
MarciXs's Avatar
 
Join Date: Oct 2003
Location: Birmingham, UK
Age: 20
Posts: 423
Default 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.
MarciXs is offline   Reply With Quote
Old 09-25-2007, 02:30 PM   #2 (permalink)
RunUO Forum Moderator
 
daat99's Avatar
 
Join Date: Dec 2004
Location: Israel
Age: 27
Posts: 8,163
Send a message via ICQ to daat99 Send a message via AIM to daat99
Default

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
-------------------------------------------------------------
daat99 is offline   Reply With Quote
Old 09-26-2007, 05:43 PM   #3 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

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.
Sep102 is offline   Reply With Quote
Old 09-26-2007, 06:41 PM   #4 (permalink)
Forum Expert
 
MarciXs's Avatar
 
Join Date: Oct 2003
Location: Birmingham, UK
Age: 20
Posts: 423
Default

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 is offline   Reply With Quote
Old 09-27-2007, 05:15 PM   #5 (permalink)
Forum Expert
 
MarciXs's Avatar
 
Join Date: Oct 2003
Location: Birmingham, UK
Age: 20
Posts: 423
Default

Quote:
Originally Posted by Sep102 View Post
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
MarciXs is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5