SAP DEV
SAP DEV

Reputation: 111

Log error message during stringWithContentsOfFile

I am trying to read a CSV file. I have imported the file in my application, so the file is in local. When I execute this part of code below:

@autoreleasepool {
    NSError *error;
    NSString *alldata =  [NSString stringWithContentsOfFile:@"config_menu_position.csv"
                                                   encoding:NSUTF16StringEncoding error:&error];
    NSLog(@"alldata: %@", alldata);
    NSLog( @"error: %@", error );
}

I have this log error message and my variable is always nul:

    2013-04-21 17:05:37.571 PageViewController[1578:c07] alldata :(null)
    2013-04-21 17:05:38.242 PageViewController[1578:c07] error: Error Domain=NSCocoaErrorDomain
 Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x7ebf100 
{NSFilePath=config_menu_position.csv, NSUnderlyingError=0x7ebf060 "The operation couldn’t be 
completed. No such file or directory"}

any idea?

Upvotes: 1

Views: 237

Answers (2)

gberginc
gberginc

Reputation: 447

Try pathForResource:ofType from NSBundle, eg.

[[NSBundle mainBundle] pathForResource:@"config_menu_position" ofType:@"csv"]

Then use thepath returned in your ANSString initializer.

Upvotes: 1

tkanzakic
tkanzakic

Reputation: 5499

You are missing the full path to the file, try this:

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"config_menu_position" ofType:@"csv"];
NSString *alldata = [NSString stringWithContentsOfFile:filePath
                                              encoding:NSUTF16StringEncoding 
                                                 error:&error];

Upvotes: 2

Related Questions