Jakob
Jakob

Reputation: 982

Modifying a large file in Cocoa

I'm trying to modify tiny chunks (32 bytes) of large (hundreds of MB) .wav audio files. Currently I load the files using

[NSData dataWithContentsOfURL:]

modify the bytes, and save the file using

[data writeToURL:].

Is there a convenient way to modify a binary file without loading it into RAM?

edit: The following stdio functions work for me:

NSUInteger myOffset = 8;
const char *myBytes = myData.bytes;
NSUInteger myLength = myData.length;            
FILE *file = fopen([[url path] cStringUsingEncoding:NSASCIIStringEncoding], "rb+");
assert(file);       
fseek(file, myOffset, SEEK_CUR);
fwrite(myBytes, 1, myLength, file);
fclose(file);

Upvotes: 0

Views: 119

Answers (1)

justin
justin

Reputation: 104728

yes, you would use a lower level approach such as fopen to avoid repeatedly loading/reloading the file via NSData (as you have found and mentioned in your update). this is the level i work at for audio file I/O.

if you want a Foundation type, you may want to try NSFileHandle.

Upvotes: 1

Related Questions