Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Kingbombs

macrumors member
Original poster
Jun 24, 2009
98
0
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.
 
One of these things is wrong.


Also, you should be using the
Code:
 tags.[/QUOTE]

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?
 
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:mad:"%@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:mad:"%@", 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:mad:"/folder/%@name", person];
and
NSString *file = [documentsDirectory stringByAppendingFormat:mad:"folder/%@name", person];
neither worked

also as a long shot i tried these:
NSString *file = [documentsDirectory stringByAppendingFormat:mad:"/random/%@name.txt", person];
and
NSString *file = [documentsDirectory stringByAppendingFormat:mad:"random/%@name.txt", person];
and
NSString *file = [documentsDirectory stringByAppendingFormat:mad:"%@name.txt", person];
and
NSString *file = [documentsDirectory stringByAppendingFormat:mad:"/%@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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.