I'm trying to teach myself (painfully) how to do file I/O in objective-c. The following code is intended to let the user enter a file name from stdin and then create a NSFileManager using that name. The code works if I hard-code the file name as an NSString (shown in the commented code @"barney.txt". However, nothing appears to happen if I use the same file name with stdin. The program will NSLog out the name of the file. But, no dice on the FileManager.
I need help.
Thanks, Mark Allyn
enter code here
#import <Foundation/Foundation.h>
int main (int argc, char ** argv)
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//NSString * fName = @"barney.txt";
NSFileManager * fm;
//NSDictionary *attr;
NSFileHandle *input = [NSFileHandle fileHandleWithStandardInput];
NSLog(@"Created file handle...");
NSLog(@"Enter File Name");
NSData *inputData = [input availableData];
NSLog(@"Created NSData object");
NSString *fName = [[NSString alloc] initWithData:inputData encoding:NSUTF8StringEncoding];
NSLog(@"Created NSString object");
NSLog(@"You entered %@ as the name\n", fName);
fm = [[NSFileManager alloc] init ]; //defaultManager
if (fm == nil)
{
NSLog(@"Failed to create file manager object\n");
}
if ([fm fileExistsAtPath:fName] == YES){
NSLog(@"File Exists!");
NSLog(@"%p", fm);
} else
{
NSLog(@"File Appears not to exist");
}
[fm release];
[pool drain];
return 0;
}