Problem with reading from files


(Magik) #1

I have a problem with reading external files.

Writing to files works great but if I want to read from files it only works once and only if I call my function directly from G_InitGame().

Here is a snippet of the code I use:


qboolean readFromFile() {
  char FILE_NAME[MAX_QPATH];
  fileHandle_t file;
  int len;

  sprintf(FILE_NAME, "%s.file", level.rawmapname);

  len = trap_FS_FOpenFile(FILE_NAME, &file, FS_READ);

  if (len <= 0) {
    G_Printf(S_COLOR_RED "File %s empty.
", FILE_NAME);
    return qfalse;
  }

  // here follows reading and parsing stuff
  [...]

  trap_FS_FCloseFile(file);

  return qtrue;
}

I call my function from G_InitGame():


void G_InitGame( int levelTime, int randomSeed, int restart ) {

  [...]

  readFromFile();
}

The thing I don’t undestand is:
If the function is called for the first time (during warmup init) it works great but after that it always reports an empty file.
For testing I added a server command to manually call the function and removed the call from G_Init but that way it didn’t even work on the first call.
I write to files in several other places of my code and writing works everytime.

I really hope someone here can help me or at least give me a hint.


([RW]FRED) #2

U forget the close file here

if (len <= 0) {
G_Printf(S_COLOR_RED "File %s empty.
", FILE_NAME);

close the file before the return
return qfalse;
}

In fact if ur file is inside pk3, the engine can’t seek backward the file pointer.

Regards


(Magik) #3

Hmm, thanks for your reply but I don’t think that’s the problem. I know the file exists and it exists outside pk3 and the function works perfectly on first init (where the files are all properly closed), so len should NEVER be <= 0.

As a workaround I have switched to using standard lib functions, which work well enough for me.


([RW]FRED) #4

But when ur file will packed in pk3 stdlibc call will. G_InitGame is called twice in the game process, one when u enter in the warmup, and one when the game start really. If ur file still opened, reopen fails. Because that file will be opened with READ|WRITE attribute and Win32 syscall throw an error.


(Magik) #5

Yes, I see but I added extra trap_FS_FCloseFile() statements to ensure all files are closed properly and still no difference. :frowning:


(Magik) #6

Ok. I think it was just that damn pure server thing… :banghead:


(Domipheus) #7

name all your files .cfg and they will bypass the pure server check