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

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

  1. macrumors member

    Joined:
    Jun 24, 2009
    #1
    Hey,

    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:
    Code:
    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:
    Code:
    NSString *myText = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"myText:%@", myText);
    
    This just prints: myText:(null)

    To write the file:
    Code:
    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)
    Code:
    NSString *file = [documentsDirectory stringByAppendingFormat:@"/folder/%@name", person];
    NSString *file = [documentsDirectory stringByAppendingFormat:@"folder/%@name", person];
    
    neither worked

    also as a long shot i tried these:
    Code:
    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. macrumors newbie

    Joined:
    Aug 11, 2010
    Location:
    ...in the UK
    #2
    One of these things is wrong.
    Also, you should be using the
    Code:
     tags.
     
  3. thread starter macrumors member

    Joined:
    Jun 24, 2009
    #3
     
  4. macrumors 603

    Joined:
    Aug 9, 2009
    #4
    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. thread starter macrumors member

    Joined:
    Jun 24, 2009
  6. macrumors newbie

    Joined:
    Aug 11, 2010
    Location:
    ...in the UK
    #6
    Resources folder != Documents folder.
     

Share This Page