Writing files on iPad doesn't work but does on simulator

Discussion in 'iOS Programming' started by Kingbombs, Aug 12, 2010.

  1. Kingbombs macrumors member

    Jun 24, 2009

    I'm trying to write to a file when i leave the view and when i load the view i want to read from that file.
    On the simulator this works fine, but on the iPad it doesn't work at all. It doesn't seem to write to the file at all or read the file.
    The file i have put in a folder called random, which i put into the Resources folder in XCode.

    the code to read and write the files is very similar:
    these lines are the same for both:
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *file = [documentsDirectory stringByAppendingFormat:@"%@name", person];
    person is just a string with an underscore after it so you could end up with person being John_ so the file is John_name.txt

    To read the file:
    NSString *myText = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"myText:%@", myText);
    This just prints: myText:(null)

    To write the file:
    NSString *myText = [[NSString alloc] initWithFormat:@"%@", text];
    [[myText dataUsingEncoding:NSUTF8StringEncoding] writeToFile:file atomically:YES];
    I did try writing to the file using the NSString write to file method, but that didn't work either so i tried using the NSData method.

    Anyone know what maybe the problem is.
    I tried (because its in a sub folder)
    NSString *file = [documentsDirectory stringByAppendingFormat:@"/folder/%@name", person];
    NSString *file = [documentsDirectory stringByAppendingFormat:@"folder/%@name", person];
    neither worked

    also as a long shot i tried these:
    NSString *file = [documentsDirectory stringByAppendingFormat:@"/random/%@name.txt", person];
    NSString *file = [documentsDirectory stringByAppendingFormat:@"random/%@name.txt", person];
    NSString *file = [documentsDirectory stringByAppendingFormat:@"%@name.txt", person];
    NSString *file = [documentsDirectory stringByAppendingFormat:@"/%@name.txt", person];
    any help would be appreciated.
  2. Anne R. Kist macrumors newbie

    Aug 11, 2010
    ...in the UK
    One of these things is wrong.
    Also, you should be using the
  3. Kingbombs thread starter macrumors member

    Jun 24, 2009
  4. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    The filename you write to, hilited in blue, is not the same as any of the filenames you read from, hilited in red.

    Furthermore, you say you wrote the file into a folder, but your code shows no such thing. And you say "so the file is John_name.txt", yet the code doesn't show that, either.

    You can't expect to use different filenames and have them refer to the same thing.

    Maybe you happen to have a file with one of the red-hilited names on the simulator. How it got there is anyone's guess, but probably from an earlier run of you program when you used a different filename.

    You should log the actual pathname you wrote to, then go look at that location.
    Do the same with the pathnames you try to read from, and see if any match.
  5. Kingbombs thread starter macrumors member

    Jun 24, 2009
  6. Anne R. Kist macrumors newbie

    Aug 11, 2010
    ...in the UK
    Resources folder != Documents folder.

Share This Page