Raised This Month: $ Target: $400
 0% 

File functions - Which to use


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
facuq
Junior Member
Join Date: Feb 2007
Old 02-09-2007 , 00:36   File functions - Which to use
Reply With Quote #1

I've been going throught the file functions in the wiki and the forum, and it's seems there's some contradiction in their use since the wiki says some functions are deprecated, but they are still mentioned here in the forums. So, could anyone please make a little list with the ones i should use? I'd really appreciate it
facuq is offline
sawce
The null pointer exception error and virtual machine bug
Join Date: Oct 2004
Old 02-09-2007 , 01:42   Re: File functions - Which to use
Reply With Quote #2

fopen/fclose/fgets/fputs and the like are the "ideal" functions to use, but they're a bit harder to use (unless you've had prior experience with them in C) than read_file/write_file.

read_file especially is a very expensive native when used in a medium to large sized file. It's fine if used only for a couple of lines, but ideally you should use fgets() (and, obviously fopen()/fclose()) instead for anything large or speed-critical.
__________________
fyren sucks
sawce is offline
facuq
Junior Member
Join Date: Feb 2007
Old 02-09-2007 , 02:40   Re: File functions - Which to use
Reply With Quote #3

Great! Thanks for the speedy reply.

Another quick question: From what i've read, I assume the difference between fgets/fputs and read_file/write_file is that while the first ones have a c like behaviour, the write_file, because it offers "line operations", it "moves" the file everytime a line has its lenght modified, or a line is added somewhere other than the EOF, and read_file has to do a sequential search counting /n to reach a line number (hence the "expensiveness").

Ok then, since I need a fixed-size record file I was hoping i could use a function like C's fread, but i guess i'll have to abuse fgets.

Again, thank you for your help!

Last edited by facuq; 02-09-2007 at 17:38.
facuq is offline
sawce
The null pointer exception error and virtual machine bug
Join Date: Oct 2004
Old 02-09-2007 , 02:57   Re: File functions - Which to use
Reply With Quote #4

fread exists (I just didn't comment about it), look in amxmodx/scripting/include/file.inc

I don't think it's well documented in the funcwiki though.

And yeah, for each read_file, core opens the file, scans to the line (a bunch of fgets()), byrefs the requested line and closes the file. write_file is similar (although write_file with a -1 line (append) is fast).
__________________
fyren sucks
sawce is offline
facuq
Junior Member
Join Date: Feb 2007
Old 02-09-2007 , 16:53   Re: File functions - Which to use
Reply With Quote #5

Yes, i saw it and fread_blocks too, but there was a comment for fwrite saying " This function doesn't work anymore in higher versions then amxmodx 1.5", that made me think the whole "fwrite/read function family" was not working. My bad.

So (to sum up): fread/fwrite fread_blocks/fwrite_blocks are acceptable?
facuq is offline
VEN
Veteran Member
Join Date: Jan 2005
Old 02-10-2007 , 04:27   Re: File functions - Which to use
Reply With Quote #6

Just a bit more info to make things even a bit more clear, since i agree that file routines a bit confusing because of some inconsistency.

I've made a diff between Function Listing for Core::file.inc and current file.cpp and that's what i see:

build_pathname - in cpp, not in the wiki

The below functions in the wiki, but not in cpp anymore:
fflush
fgetf
fgeti
fgetl
fputf
fputi
fputl
rewind
VEN is offline
facuq
Junior Member
Join Date: Feb 2007
Old 02-10-2007 , 15:35   Re: File functions - Which to use
Reply With Quote #7

Great, thank you both!
facuq is offline
Reply



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 00:36.


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