-2

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;
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
mark allyn
  • 17
  • 4
  • What's the actual problem? Which logs do you see? – rmaddy May 18 '15 at 21:01
  • BTW - if you are playing with command line level code and dealing with `stdin`, why are you using iOS and not doing this on the Mac as an OS X app? – rmaddy May 18 '15 at 21:02
  • The actual problem is that the program prints "File appears not to exist". – mark allyn May 18 '15 at 21:05
  • If I had a Mac I would. But, I'm running on a linux box (Ubuntu 12.04). I'm compiling with GNUstep. – mark allyn May 18 '15 at 21:06
  • Then the file doesn't exist - at least relative to the app's current working directory. – rmaddy May 18 '15 at 21:06
  • Then why is this tagged iOS? You can't be building an iOS app. – rmaddy May 18 '15 at 21:07
  • If it doesn't exist, then why is it that if I hard code the file name in as NSString * fName = @"barney.txt" the program runs as expected. The working directory should be the same, shouldn't it? – mark allyn May 18 '15 at 21:09
  • My apologies for mistakenly including the iOS tag. – mark allyn May 18 '15 at 21:10
  • What do you log for `fName` when using `stdin`? Make sure there is no newline or other whitespace in the name. – rmaddy May 18 '15 at 21:11
  • If I understand your log question, the answer is that NSLog(@"fName"); will print out to stdout the file name I entered on stdin. – mark allyn May 18 '15 at 21:15
  • I'm asking what it actually logs. What is the exact output from your `NSLog` statement? – rmaddy May 18 '15 at 21:16
  • 2015-05-18 17:25:03.678 FileCheck[12885] File Appears not to exist – mark allyn May 18 '15 at 21:25
  • Sorry, wrong NSLog. The correct NSLog was: 2015-05-18 17:25:03.678 FileCheck[12885] You entered barney as the name – mark allyn May 18 '15 at 21:26
  • "barney" and "barney.txt" are not the same thing. Did you try to enter "barney.txt"? – rmaddy May 18 '15 at 21:40
  • Sorry for the red herring! You're right, but there are two files, barney.txt and barney. When I ran the program last time I typed in just barney on the keyboard. Hence, the output I showed. However, if I do the same thing with barney.txt on the keyboard it will LOG out the same message. So, here it is, this time with barney.txt. 2015-05-18 17:44:13.551 FileCheck[12948] You entered barney.txt as the name – mark allyn May 18 '15 at 21:44
  • I added a single line of code to the above program which prints out the heap location of the NSFileManager instance. The code is NSLog(@"fm maps to %@", fm) . When I do that right after the attempt to instantiate fm, the resulting output appears to indicate that I actually have created the desired NSFileManager. The output reads: 2015-05-18 18:13:44.183 FileCheck[13162] fm maps to . So, the program is failing at the line: if ([fm fileExistsAtPath:fName] == YES)... or so it appears. – mark allyn May 18 '15 at 22:19
  • The answer to my question is simply that using stdin to name the file puts a newline at the end. This messes up the search for the file. If you delete the newline the code will run as intended. – mark allyn May 19 '15 at 19:23
  • I mentioned that in my 5th comment above. – rmaddy May 19 '15 at 19:25

1 Answers1

0

Using stdin puts a '\n' at the end of the string. This messes up the search. If you delete the newline chars the code will run as expected.

mark allyn
  • 17
  • 4