Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stringWithContentsOfFile:encoding:error: error 260

I've got a small utility application that parses a csv-file and uses the data to fill a Core Data store for use with another application. I open the csv-file using -initWithContentsOfFile:encoding:error: and the method always returns nil with the error

Error Domain=NSCocoaErrorDomain Code=260 UserInfo=0x100106450 "The file “data.csv” couldn’t be 
opened because there is no such file." Underlying Error=(Error Domain=NSPOSIXErrorDomain Code=2 "The 
operation couldn’t be completed. No such file or directory"), {
NSFilePath = "/Users/****/Library/Developer/Xcode/DerivedData/CreateData-
bhkmspyczgcfcrgehcbaydwdbfoa/Build/Products/Debug/data.csv";
NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=2 \"The operation couldn\U2019t be 
completed. No such file or directory\"";
}

Now, I'm looking at that exact file in that exact directory. Even more confusing is the fact that I have another version of essentially the same utility for another app that works -- the only difference is the makeup of the core data store and entities and the number of columns in the csv file, all of which are invoked after loading the csv file. Or failing to load, as it were ...

int main (int argc, const char * argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSError *error = nil;
    NSString *csvFile = [[NSString alloc] initWithContentsOfFile:csvFilePath(QUESTIONS_INPUT_FILENAME) encoding:NSASCIIStringEncoding error:&error]; // This fails

    if (error != nil) {
        NSLog(@"Returned error:\n%@, %@", error, [error userInfo]); // Returns the above error message
        exit(1);
    }

// ...
}

NSString *csvFilePath(NSString *inputFileName) {
    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    return [resourcePath stringByAppendingPathComponent:inputFileName];
}

NSString *applicationDocumentsDirectory() {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
like image 831
royen Avatar asked Jan 19 '23 14:01

royen


2 Answers

It fails because you aren't passing in the actual location of the file, just the filename, so it is trying to look for it in the program's own folder - you're using the path of the bundle. unless that's where your csv file is, it won't find it.

like image 179
Abizern Avatar answered Jan 28 '23 15:01

Abizern


To put it simply the file path you passed is incorrect thus can't be found or does not exist.

like image 33
redwud Avatar answered Jan 28 '23 15:01

redwud