Archived

This topic is now archived and is closed to further replies.

MutteringGoblin

Windows file functions

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites