#### Archived

This topic is now archived and is closed to further replies.

# ifstream problems

This topic is 5727 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am opening a file to read in config information. The read is performed within a function called Load() inside a class. This is the function in the character class:

// Open the file

ifstream characterFile("characterData\\player.dat");

// If file is not opened output the error

if (!characterFile) {
Debugger.appendLog("Error opening character file.");
}

// Grab the get position in the stream

long startPosition=characterFile.tellg();
// Output the get position to logfile

// Read lines from the file

while (!characterFile.eof()) {
char buffer[256];
characterFile.getline (buffer,256);
}

// Close the file

characterFile.close();

}

I create two classes in my main program:

CCharacter myCharacter;
CCharacter otherCharacter;

and then call them like this:


The first one works fine and loads the correct information from the file, but the second one doesnt. It reads nothing from the file. The output to the debug logfile, says it opened the files fine both times, but the get position is 0 on the first attempt and -1 on the second attempt. Ive tried all things to see if the file is being opened correctly fail(), good() bad() etc. etc. & I believe im using the correct #include <fstream> because it works fine on the first call. I am at a brick wall, any ideas would be greatfully accepted!!! [edited by - redgenie on October 16, 2002 5:06:55 PM] [edited by - RedGenie on October 16, 2002 6:05:51 PM]

##### Share on other sites
Try calling clear on the ifstream object at the top of the Load method (or after calling close). It''s possible that the failbit is set on characterFile for some reason...

##### Share on other sites
Nope unfortunately that doesnt work.

This is such an annoying problem, I know its gonna be something REALLY silly, but its had me for hours now!!

##### Share on other sites
The problem is definately down to the fact that the following lines:

long startPosition=characterFile.tellg();

Report a 0 value for the tellg() on the first attempt and a -1 value on the second attempt even though it is reading from the same file.

Its almost as if the file is locked or the characterFile handle is somehow corrupt from the first call. Im clutching at straws because surely they are not related at all, they are two seperate instances of the class.

Grrrr

[edited by - RedGenie on October 16, 2002 6:05:02 PM]

##### Share on other sites
Update, im still having the problem only I thought I would try this:

I created an exact copy of the load() function and called it spawn()

Now the first character i create using:

and the second character using:

otherCharacter.spawn();

I thought it might be that it was "sharing" some variable memory space or something similar but alas exactly the same thing happens.......

##### Share on other sites
Unbelievable, I knew it was something SILLY!!

It was because I was using this inside the Debugger.appendLog() function:

freopen (theLogfile,"a",stdout);
printf (label);
printf (theStr);
printf ("\n");
fclose (stdout);

Now it works fine!! Ive replaced the above with ofstream and it works fine!

*Sigh*

• 11
• 20
• 12
• 10
• 34
• ### Forum Statistics

• Total Topics
631399
• Total Posts
2999853
×