reading file


(s-men) #1

okay I got this code to read a file on the server side:

char *g_ReadFiles(char *filename)
{
	int		len;
	fileHandle_t	f;
	char		*buf;

	len = trap_FS_FOpenFile( filename, &f, FS_READ );
	if ( !f ) {
		return va( S_COLOR_RED "file not found: %s
", filename );
	}
	trap_FS_Read( buf, len, f );
	//buf[len] = 0;
	trap_FS_FCloseFile( f );
	return buf;
}

and i’m calling it with this:

trap_Printf(g_ReadFiles("file.txt"));

is this correct and if so, where the hell do I have to put the text file to make it work? :stuck_out_tongue:
The files i’m trying to read are supposed to be on server side only.


(kamikazee) #2

The .txt file should be in a .pk3 if your server was started with sv_pure set to 1.

If it is set to 0, just place it in your mod folder or some other subdir.


(Mr.Mxyzptlk) #3

is this correct and if so, where the hell do I have to put the text file to make it work? :stuck_out_tongue:
The files i’m trying to read are supposed to be on server side only.

Since your file is server-side only, you need not place it in a PK3. Try placing the file inside your fs_game subdir and it should be available for reading (and writing) using the engine API.


(Rudi) #4

He tried that but it wasn’t working either, he needs the clarification on what functions to use when writing/reading files (Infact I think he can write files already), is the function about correct?


(Mr.Mxyzptlk) #5

Sure. Use trap_FS_FOpenFile() to open. The return value is file length in bytes. IIRC, len will be -1 if does not exist and fileHandle_t will be 0 if file was not opened (ie: does not exist).

Use trap_FS_Read() to read an open file. g_ReadFiles() as shown above is faulty – it is passing an undefined pointer, instead of a pointer to a real buffer. The buffer must be large enough to hold len bytes as specified by 2nd argument.

Take a look at G_ParseAnimationFiles() in the SDK for a simple example of how to read files.

edit: corrected return values for opening file which does not exist


(s-men) #6

I took a look at G_ParseAnimationFiles()
and then used the same way of declaring the buffer there

static char filetext[100000];

so filetext now replaces the buf in my previous posted code.
so now i got this in there:

int			len;
	fileHandle_t	f;	

	len = trap_FS_FOpenFile( filename, &f, FS_READ );
	//if ( !f ) {
	//	return va( S_COLOR_RED "file not found: %s
", filename );
	//}
	if( len <= 0 ) {
		return va( S_COLOR_RED "file not found: %s
", filename );
	}
	if ( len >= sizeof( filetext ) - 1 ) {
		return va( S_COLOR_RED "File %s is too long
", filename );
	}
	trap_FS_Read( filetext, len, f );
	filetext[len] = 0;
	trap_FS_FCloseFile( f );
	return filetext;

now problem is that I always get back file not found, meaning he went in the if, and returned from there, never to actually read it, so that wasn’t the problem, does the filename has to be specefied in special ways?


(Mr.Mxyzptlk) #7

what is the full path to your ET install?
and what is the value of filename?


(s-men) #8

C:\Program Files\Wolfenstein - Enemy Territory
and i’m trying with censor.txt
so the value of filename is “censor.txt”


(Mr.Mxyzptlk) #9

and unless you have a custom fs_game name going on (eg: for mod purposes),
try putting the file in etmain/ subdir.


(Rudi) #10

It does have a custom fs_game set :slight_smile:


(s-men) #11

actually the file is in the mod folder and in etmain folder

edit: I renamed the one in etmain, and i’ve called the function twice with etmain filename one and mod filename the other and both were not found


(s-men) #12

nobody who can give the tip to solving my problem??? :stuck_out_tongue:


(s-men) #13

lol you were totally right, I should have tried this first.
But the weird thing was that i already tried to put it in a pk3 once and it has no result then, but it definitly worked now, so thanks!


(Rudi) #14

But arn’t PK3s mainly used client side?? And server side files need to be out of the PK3?? (like the DLLs/SOs)


(Mr.Mxyzptlk) #15

Yup, pk3 files are (usually) for files which need to be in sync for both server/client. Any files placed inside a pk3 which are server-only are:

  1. not writable
  2. wasted bandwidth when downloaded to clients

(Rudi) #16

Thought so, but if it only works when not in the PK3 with sv_pure set to 0, how can you get around this limitation? (as most servers run with sv_pure 1)


(Mr.Mxyzptlk) #17

You’ve probably just overlooked something very simple and no one is seeing it. For eons mods have been using the trap calls (engine API) for opening server-only files (with sv_pure 1), the most well known example is probably shrubbot.cfg . So take a look at ETPub source code for their usage of engine calls reading/writing that file for yet another example. For Jaymod we use std::iostreams instead of the engine API, but that’s because we converted to C++ a long time ago.


(Rudi) #18

Would it be viable to include the standard library in just the one file where we are needing to read a file or not??

And what you said before too, that may be true, but if so why does it still read it from the PK3 with sv_pure 0?? :?


(Mr.Mxyzptlk) #19

Don’t bother with the C++ stuff, thats way too complicated to switch to for SDK.

Ok I added the following block of code to test the file API in Jaymod in G_InitGame().
Please note this codeblock will shutdown the process (no game will start) as all it is doing is testing a file read.

The file is server.cfg which is in my case located in jaymod/ subdirectory. In your case (I am guessing here) it is probably etmain/ subdir. The code works fine, finds my file and prints it no problemo. From this working piece of code you should be able to get it to do what you want.

    {
        int len;
        fileHandle_t f;
        char buffer[10240];

        len = trap_FS_FOpenFile( "server.cfg", &f, FS_READ );
    
        G_Printf( "result: len=%d f=%d
", len, f );

        if (f) {
            if (len < sizeof(buffer)) {
                trap_FS_Read( buffer, sizeof(buffer), f );
                buffer[len] = 0; // null terminate buffer for safety
                G_Printf( "CONTENTS:
%s
", buffer );
            }
            trap_FS_FCloseFile( f );
        }
exit( 1 );
    }


(Rudi) #20

Thanks alot for that!!

I’m sure s-men will test it tonight (and even I might! :o)!!!

Thanks again Mr.M!