I am working on a code that, among other things, must save a zero-filled file. It can expect to create a file from 10MB to even 1GB blank.
It must be similar to this Unix command:
dd if=/dev/zero of=my_image.dsk count=20480
I could do this one work with small sizes:
int totalSize = 1024*1024;
char buf[totalSize];
strcpy(buf,"");
NSData *theData = [NSData dataWithBytes:buf length:sizeof(buf)];
//NSLog(@"%@", theData);
NSLog(@"%lu", sizeof(buf));
[theData writeToFile:@"/Users/foo/Desktop/my_image.dsk" atomically:YES];
But if I try a bigger value (1024*1024*10, for instance), it crashes. So I tried:
NSFileManager *ddd = [[NSFileManager alloc ] init];
[ddd createFileAtPath:@"/Users/foo/Desktop/my_image.dsk" contents:[NSData data] attributes:nil];
It creates an empty file, but is not good because the file size is zero. It should not be zero.
I spent hours trying to find an answer without success. I want to do this in Obj-C, but C is also an option before I go nuts.
Please, someone give-me some light!
Thanks in advance!
-- Edit --
Thanks everyone, but one more thing: is it possible to write without allocating everything on memory?