AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Read the entire file into a variable (https://forums.alliedmods.net/showthread.php?t=159027)

-=hunter=- 06-12-2011 01:17

Read the entire file into a variable
 
How to do this? I tried so:
PHP Code:

static stream[51000]
new 
hFile fopen(g_TopFile"rb")
fread_raw(hFilestream500100)
fclose(hFile)

new 
text[400]
formatex(text399"stream = %s"stream)
server_print(text

but outputs other text

fysiks 06-12-2011 01:24

Re: Read the entire file into a variable
 
Have you tried reading it as text instead of as binary?

Maybe a better question is why do you need to read so much data at one time? Aka What are you trying to do?

-=hunter=- 06-12-2011 01:33

Re: Read the entire file into a variable
 
Is no longer necessary. I thought that reading the file will take less time than when using fgets but I was wrong.

Exolent[jNr] 06-12-2011 01:49

Re: Read the entire file into a variable
 
You could do it like this:

Code:
stock file_get_contents(const filename[], output[], maxlen) {     output[0] = EOS;         new f = fopen(filename, "rt");         if(!f) return 0;         new len;         while(len < maxlen && !feof(f))     {         len += fgets(f, output[len], maxlen - len);     }         fclose(f);         return len; }

Code:
new contents[2500]; new len = file_get_contents("motd.txt", contents, charsmax(contents)); // contents = contents of the "motd.txt" file // len = length of contents string


All times are GMT -4. The time now is 23:32.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.