StringWithContentsOfFile: encoding: error: error 260

I have a small utility application that parses a csv file and uses the data to populate the Core Data storage for use with another application. I open a csv file with -initWithContentsOfFile:encoding:error: and the method always returns nil with an 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 am browsing this exact file in this exact directory. Even more confusing is the fact that I have a different version of essentially the same utility for another application that works - the only difference is the creation of the main data and entity store and the number of columns in the csv file, all of which are called after the csv file is loaded . Or failed 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]; } 
+4
source share
2 answers

This fails because you are not transferring the actual location of the file, just the file name, so it tries to find it in the programโ€™s own folder - you are using the set path. if only where your csv file is located, it will not find it.

+8
source

Simply put, the path to the file you submitted is incorrect, so it cannot be found or does not exist.

+1
source

All Articles