#### Archived

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

# 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 on other sites
Shannon Barber    1681
If you just write to the file using the standard C functions (or C++ streams), Windows will buffering everything for you.

##### Share on other sites
evillive2    779
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.