ifstream problems

Started by
4 comments, last by RedGenie 21 years, 6 months ago
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:
    
void CCharacter::Load() {
  // 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

  Debugger.appendLogValue("Read position is ",startPosition);

  // 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:

      
myCharacter.Load();
otherCharacter.Load();
    
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]
Advertisement
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...
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!!
The problem is definately down to the fact that the following lines:

long startPosition=characterFile.tellg();
Debugger.appendLogValue("Read position is ",startPosition);

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]
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:

myCharacter.load();

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

This topic is closed to new replies.

Advertisement