Skip to main content
gamedev.net
🔒 Locked

Windows file functions

Started by MutteringGoblin Apr 29, 2003 at 9:05 PM 2 replies 500+ views
Original Post
MutteringGoblin
MutteringGoblin
Hello again, There''s a WinAPI function that resizes an open file - I don''t have the docs on this computer but I think it''s SetEndOfFile or something... Anyway I was wondering is it okay to resize a file frequently by small amounts, or should I resize it in blocks? If so what''s the best amount to resize by? 256 bytes? 1024? I often have to write an unknown amount of data to files, so I''d like my file class to extend the file automatically when needed. Obviously resizing the file all the time is the lazy solution but I guess if it''s okay and it''s fast enough I''ll do that.
Shannon Barber
Shannon Barber
If you just write to the file using the standard C functions (or C++ streams), Windows will buffering everything for you.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
evillive2
evillive2
are you adding to the end of a file or inserting in the middle? Inserting might be a little complicated if your not willing to re-write the entire file but adding (appending) to the end of file is as simple as opening the file using the "a" or append flag.

FILE *fp = fopen( "file.txt", "a" );
fprintf( fp, "adding a line to the end of a file\n" );
fclose(fp);

if "file.txt" does not exist it will be created for you, but if it does exist it opens the file and allows you to write to it starting at the end of whatever was in the file. using the append flag you don''t have to worry about placing the EOF marker as it is automaticly moved ahead as is the purpose of appending to a file.
Evillive2
MutteringGoblin
MutteringGoblin
Hi all,

Thanks but I''ve already written about 200 lines of code using the WinAPI functions so I don''t really want to switch to the standard libs now! I decided it would be better in the long run to use the Windows functions because they let you get the contents of folders and stuff. (I don''t think the standard libs will do that...?)

Doesn''t matter I guess I''ll just do some speed tests and find out which way is faster.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.