how to deal with files
how to deal with files
- Subject: how to deal with files
- From: Pascal Goguey <email@hidden>
- Date: Sat, 23 Nov 2002 01:52:45 +0900
Hello,
I am new to Cocoa, and I am trying to figure out how to work with
files. I wrote a simple test. One GUI with a single button. One object
(see below). The button calls the method WriteFile, and that's
about it.
Everytime I push the button (the only one), it should write one
line at the end of the file. The code of the object is listed below.
The problem is that the program exits after the first button press.
Any hint?
Thanks,
Pascal
---------------
/* FileObject */
#import <Cocoa/Cocoa.h>
@interface FileObject : NSObject {
IBOutlet id mWriteButton;
NSFileHandle * mFileHandle;
}
- (IBAction)WriteFile:(id)sender;
@end
---------------
#import "FileObject.h"
@implementation FileObject
- (void)awakeFromNib {
mFileHandle = NULL;
}
- (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 write
Data: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.