Raised This Month: $ Target: $400
 0% 

Help about Reading from file strings.


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-28-2016 , 20:16   Re: Help about Reading from file strings.
Reply With Quote #7

fgets() returns the number of characters read in the line, when you reach the end of the file it will output 0. feof() is still the proper choice, even though using fgets() as the condition technically could still work.

Code:
// native fgets(file, buffer[], maxlength);
static cell AMX_NATIVE_CALL amx_fgets(AMX *amx, cell *params)
{
	FileObject* fp = reinterpret_cast<FileObject*>(params[1]);

	if (!fp)
	{
		return 0;
	}

	static char buffer[4096];
	buffer[0] = '\0';

	fp->ReadLine(buffer, sizeof(buffer) - 1);

	return set_amxstring_utf8(amx, params[2], buffer, strlen(buffer), params[3]);
}
Using a file named "test.txt" with the below contents:
Code:
AAAAA
BBBBB
CCCCC
it gives the below output (notice it will read the new line character)
Code:
6 chars = AAAAA

6 chars = BBBBB

5 chars = CCCCC
EOF reached
PHP Code:
new iFile fopen"test.txt" "rt" );
new 
szTest10 ] , iChars;
    
while ( ( 
iChars fgetsiFile szTest charsmaxszTest ) ) ) )
{
    
server_print"%d chars = %s" iChars szTest);
}
    
fcloseiFile );
    
server_print"EOF reached" ); 
__________________

Last edited by Bugsy; 01-28-2016 at 20:21.
Bugsy is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 09:21.


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