RE: how to deal with files
RE: how to deal with files
- Subject: RE: how to deal with files
- From: "Jonathan E. Jackel" <email@hidden>
- Date: Fri, 22 Nov 2002 14:07:03 -0500
Data is autoreleased when it is created. You did not use alloc, copy, or
new to create it, so you should not release it. When you release it anyway
at the end of WriteFile:, your program crashes.
Incidentally, I believe initWithCString is deprecated. There's no real
point to using a C string in this context anyway. Use
Path = [NSString stringWithString:@"/Users/pascal/Desktop/helpfile"]
Just remember the @. You could even do
Path = @"/Users/pascal/Desktop/helpfile"
Either way, get rid of [Path release].
Jonathan
>
- (IBAction)WriteFile:(id)sender {
>
NSString * Path = [[NSString alloc]
>
initWithCString:"/Users/pascal/Desktop/helpfile"];
>
NSData * Data = [NSData dataWithBytes:"help!\n" length:6];
>
// Create the file object if it does not exist yet
>
if(mFileHandle == NULL) {
>
// Check if the file itself exists at the
>
specified path. If not,
>
create it
>
NSFileManager * mFileManager = [NSFileManager
>
defaultManager];
>
if([mFileManager fileExistsAtPath: Path] == NO) {
>
[mFileManager createFileAtPath:Path
>
contents:nil attributes:nil];
>
}
>
// Then at this point, the file exists physically
>
on the disk
>
mFileHandle = [NSFileHandle
>
fileHandleForUpdatingAtPath:Path];
>
}
>
[mFileHandle seekToEndOfFile];
>
[mFileHandle writeData:Data];
>
[mFileHandle closeFile];
>
[mFileHandle release];
>
mFileHandle = NULL;
>
[Path release];
>
[Data release];
>
}
>
>
@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.