Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to truncate x bytes from file

Quite simply, how do I remove x amount of bytes, or a string from (the end of) a file..? I know how to append bytes — I need to do the opposite.

Unfortunately I haven't found any examples of how to do this :-/

NSFileHandle* fh = [NSFileHandle fileHandleForUpdatingAtPath: path];
    [fh seekToEndOfFile];

    ??
like image 218
Hugo Voss Avatar asked Jan 13 '13 04:01

Hugo Voss


2 Answers

NSFileHandle has a truncateFileAtOffset method that does exactly what you want.

like image 59
Carl Norum Avatar answered Sep 26 '22 13:09

Carl Norum


Given the NSString sFilePath variable is set, this is how you could truncate the file to 0 bytes:

NSFileHandle *hFile;
hFile = [NSFileHandle fileHandleForUpdatingAtPath:sFilePath];
[hFile truncateFileAtOffset: 0];
[hFile closeFile];
like image 24
Volomike Avatar answered Sep 26 '22 13:09

Volomike