The size of a file is simply determined by its contents, you can only influence it by writing stuff to the file. File size is normally not an independent variable that you can set at arbitrary values. |
Err... wouldn't it
have to be an independent variable? How else would the file system know how large a file is? There's no way to determine that from contents alone. There's no special "EOF" byte or string that marks the end of the file.
It's not an independent variable
in cstdio (at least not one that's user accessable), which is my point. cstdio isn't good enough for what I want to do. That's why I used the WinAPI method of File I/O on Windows -- and why I'm looking for alternatives for Linux.
Can you indicate for what kind of application you would "set" the size of a file? As it is now, your problem does not make any sense. |
Okay -- let's say I'm making a hex editor. The user goes in and deletes the last 5 bytes off a 2 GB file. How could this be accomplished without reading the entire file into memory, then writing it back (a process that could possibly take several minutes, and would use tons of RAM)
And to pre-empt the file mapping suggestion: that's no good because it doesn't allow you to buffer changes made to the file.
what you can do is, seek to the byte and then write something there. this will create a hole in the file and you have that much size of file. |
This works for making a file larger, but this method can't be employed to make a file smaller, which is typically what I'd want to do this for (not to mention this seems a little hackish to me -- but I guess if it's standard behavior it's fine).
So far the only way I'm aware of to make a file smaller with cstdio is to read the entire file into memory (minus what I want to remove), then re-open the file with "w" to wipe it clean, then write everything back. That is such a huge waste of time and resources when the same thing should be able to be accomplished with a very minor function to change the filesize.