0

I have text files that contain some basic data that I need for my app. I can read the files and I get the file path with:

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Money" ofType:@"txt"];

To write to the file I would think I would use:

[[HoldString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileAtPath atomically:NO];

This does not work though, I have also tried geting the file path with:

NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* fileAtPath = [filePath stringByAppendingPathComponent:@"Money.txt"];

None of these work. I would like to be able to read from the file not using CoreData.

Jim Puls
  • 79,175
  • 10
  • 73
  • 78
Alex Pelletier
  • 4,933
  • 6
  • 34
  • 58

3 Answers3

1

The resource bundle of an app is read-only (at least on a real device). You need to write your data to another location such as the Documents directory.

Most apps check to see if the file exists in the Documents directory. If not, it copies a file from the resource bundle. Then all reading and writing is done to the copy.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
1

You can't write to paths that are inside your app bundle; your bundle is readonly. So that's why the first approach won't work.

But your second approach should in fact work to form a valid path in the documents directory. Are you sure there's something there to read? That directory will be empty when your app is installed. If you want to modify a text file that you yourself include, first copy it from the bundle path to the documents folder.

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
  • Were is the document folder? i had this code if (![[NSFileManager defaultManager] fileExistsAtPath:fileAtPath]) { [[NSFileManager defaultManager] createFileAtPath:fileAtPath contents:nil attributes:nil]; } – Alex Pelletier Mar 20 '13 at 00:19
  • @user1713165: The documents folder exists on disk. If you're using the simulator, you can browse to it with Finder on your Mac. In no case is the user able to interact with it directly; it's just a good location to put your app's documents when necessary. – Ben Zotto Mar 20 '13 at 00:26
  • @user1713165: The code snippet in your comment doesn't actually do anything. Creating a file with `nil` contents is meaningless. Nothing will be written to disk. – Ben Zotto Mar 20 '13 at 00:27
  • Ok i understand how the document folder works now, it creates the file at different paths depending on the device and such, right? So I did some testing and it workings well thanks. – Alex Pelletier Mar 20 '13 at 00:31
0

On iOS the application bundle is readonly, so you can't write on it. A way to go around this is to use the documents folder to store the files that you need and write data there.

In your case an alternative would be to use NSUserDefaults. So the first time that the application runs you can read from that file (Money.text) on the bundle (or avoid holding it on the bundle if you can do so). After reading it, instead of writing to it, you save the whole text to NSUserDefaults like this:

[[NSUserDefaults standardUserDefaults] setObject: holdString forKey: @"money" ];  // By convention you should name it holdString instead of HoldString.  

To read data:

NSString* moneyStr= [[NSUserDefaults standardUserDefaults] objectForKey: @"money"];
Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187