PDA

View Full Version : Big problem with writeToFile !




bboyjayz
Jun 7, 2009, 07:01 PM
hi,
i've a problem, i try to save strings i read since a fichier.txt file on the web, and save it into my iPhone, it's work fine, but when i call my saved file since another class, it's seems to be empty, here is my source code to save my file:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"random.txt"];

if([string writeToFile:filePath atomically:YES encoding:NSUTF16BigEndianStringEncoding error:NULL]){
NSLog(@"OK");
}
NSLog([NSString stringWithContentsOfFile:filePath encoding:NSUTF16BigEndianStringEncoding error:NULL]);
//this NLog work fine, i see my saved data.



And i want to read it since another class, here is my code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

//NSString *path = [[NSBundle mainBundle] pathForResource:aPath ofType:@"txt"];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"random.txt"];

NSString *string = [NSString stringWithContentsOfFile:filePath encoding:NSUTF16BigEndianStringEncoding error:NULL];
NSLog(string);
NSLog(@"Do am i here?");

Even the "Do am i here" does'nt work ... when i look the Console, i can see that to save my file, it switch task...i don't know how to do...please help...



kainjow
Jun 7, 2009, 09:33 PM
First off, I'd suggest making a function/method for getting the path of that file, so that you don't have duplicate code and potentially introducing a bug from a simple typo.

Second, does NSFileManager's fileExistsAtPath: method show that it's there?

bboyjayz
Jun 8, 2009, 01:01 AM
So this morning (I'm french and in France.. ) i've seen your answer and that show me how to test the filepath....so thank you for your answer 'cause know it works fine XD the error wasn't caused by the code i past... :(

Thank you very much...

PS: i love macrumors <3