I have problems to write big files on iPhone. My code works very well on the simulator, but on the iPhone nothing happens, respectively the files have size 0! I think it depends on the document directory, but i'm not sure.
for (int i = 1; i <= [_anzahl.text intValue]; i++)
{
NSString *book = [@"book" stringByAppendingFormat:@"%i", i];
NSLog(@"%@", book);
NSString *dummyFilePath = [[NSBundle mainBundle] resourcePath];
NSString *filePath = [dummyFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", book]];
NSFileHandle *file = [NSFileHandle fileHandleForWritingAtPath:filePath];
if (file == nil) {
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
file = [NSFileHandle fileHandleForWritingAtPath:filePath];
}
int zahl = [_sizeMB.text intValue];
[file seekToFileOffset:zahl * 1024 * 1024];
const unsigned char bytes[] = {1};
[file writeData:[NSData dataWithBytes:bytes length:sizeof(bytes)]];
//Dateigröße ermitteln
int fileSize = [file seekToEndOfFile];
NSLog(@"book Size =%i" , fileSize) ;
}
Update for all newbies:
NSString *dummyFilePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];