PDA

View Full Version : Writing files on iPad doesn't work but does on simulator




Kingbombs
Aug 12, 2010, 06:42 AM
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:

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.



Anne R. Kist
Aug 12, 2010, 09:51 AM
One of these things is wrong.
The file i have put in a folder called random, which i put into the Resources folder in XCode.

...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

Also, you should be using the [code] tags.

Kingbombs
Aug 12, 2010, 11:11 AM
One of these things is wrong.


Also, you should be using the [code] tags.

How is it wrong?
Don't i want to get a reference to the documents directory and then search from there?
And if it is wrong, why would it work on the simulator and not on the devie?

chown33
Aug 12, 2010, 11:17 AM
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:
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];
and
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];
and
NSString *file = [documentsDirectory stringByAppendingFormat:@"random/%@name.txt", person];
and
NSString *file = [documentsDirectory stringByAppendingFormat:@"%@name.txt", person];
and
NSString *file = [documentsDirectory stringByAppendingFormat:@"/%@name.txt", person];

any help would be appreciated.

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.



And if it is wrong, why would it work on the simulator and not on the devie?

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.

Kingbombs
Aug 12, 2010, 11:55 AM
thanks, fixed it

Anne R. Kist
Aug 12, 2010, 12:09 PM
How is it wrong?
Resources folder != Documents folder.