Big problem with writeToFile !

Discussion in 'iOS Programming' started by bboyjayz, Jun 7, 2009.

  1. bboyjayz macrumors member

    Joined:
    Mar 20, 2009
    #1
    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:mad:"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:mad:"txt"];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:mad:"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...
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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?
     
  3. bboyjayz thread starter macrumors member

    Joined:
    Mar 20, 2009
    #3
    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
     

Share This Page