Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective c: NSString NSData reading from file problem

Tags:

objective-c

I'm using:

NSData *output1 = [NSData dataWithContentsOfFile:@"~/centralUtilOut.tmp"];
NSString *output = [[NSString alloc]initWithData:output1 encoding:NSUTF8StringEncoding];

NSLog(@"%@", output);
[output release];

But nothing is in the debug window.

This is in objective C.

Note: centralUtilOut.tmp is a normal text file

like image 795
Daniel Avatar asked Aug 12 '10 03:08

Daniel


2 Answers

The problem is in the path specification.

It seems that the NSData -dataWithContentsOfFile: does not expand ~.

It works when you use full path or expand tilde in path:

NSData *output1 = [NSData dataWithContentsOfFile:
                      [@"~/centralUtilOut.tmp" stringByExpandingTildeInPath]];
NSString *output = [[NSString alloc]initWithData:output1 
                                        encoding:NSUTF8StringEncoding];

NSLog(@"%@", output);
[output release];
like image 153
stefanB Avatar answered Oct 05 '22 02:10

stefanB


That tilde in the path makes me think your file path might not be getting handled properly. Take a look at NSString's -stringByExpandingTildeInPath method to expand the path to the full, absolute path.

For example: NSData *output1 = [NSData dataWithContentsOfFile:[@"~/centralUtilOut.tmp" stringByExpandingTildeInPath]];

like image 24
Collin Allen Avatar answered Oct 05 '22 02:10

Collin Allen