17

I am trying to take content from one file and write it into another. I am reading fine, but I am not able to write it into another file.

I have a database of words. I want to separate the words into different files based on the number of letters. All four letter words go into one file, and so on. I added a txt file called "4letter" into my resources and the following is my code:

NSError *error;

//READ
NSString *dbFile = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"txt"];
NSString *test = [NSString stringWithContentsOfFile:dbFile encoding:NSUTF8StringEncoding error:&error];

//convert from string to array
NSArray *lines = [test componentsSeparatedByString:@"\n"]; 

NSFileHandle *logFile = nil;
logFile = [NSFileHandle fileHandleForWritingAtPath:[[NSBundle mainBundle] pathForResource:@"4letter" ofType:@"txt"]];

//Test if write works
for (int i=0; i<5; i++) 
{
    NSString *randomAnagram = [[lines objectAtIndex:i] lowercaseString];
    [logFile writeData: [randomAnagram dataUsingEncoding: NSNEXTSTEPStringEncoding]];
}
paulmelnikow
  • 16,895
  • 8
  • 63
  • 114
Abhinav
  • 263
  • 1
  • 3
  • 8

5 Answers5

26

In iOS, you can't write into a file in your app's bundle -- the entire bundle is read-only. Use a path into the Documents folder instead.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 1
    What is the path to the documents folder? My target is the iOS. After writing into it where can I find the folder? – Abhinav May 02 '11 at 02:41
  • 4
    See [Standard System Directories](http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/LowLevelFileMgmt/Articles/StandardDirectories.html#//apple_ref/doc/uid/20001279-SW4) to learn how to locate the Documents directory, as well as other standard directories. – Caleb May 02 '11 at 02:49
  • 1
    To get a path to the documents folder use: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; – OscarVGG Feb 03 '13 at 16:18
  • 1
    Even if you could write to the bundle, the bundle is not backed up and your app's data would be lost. – BergQuester Oct 11 '13 at 21:17
  • Is it document folder is secure for my data. I want same security like app bundle with edit facility. Is it possible? – python Feb 25 '14 at 13:49
  • @python Please start your own question where you can be more specific about what you want. A large part of the "security" of the app bundle comes from the fact that it's not writeable, so a folder that *is* writeable obviously lacks that. – Caleb Feb 25 '14 at 14:00
  • @Caleb I just want others cant get my imp data easily or need security for my file will be secure no one can use that data in other app. – python Feb 25 '14 at 14:14
  • @python So why not create a question and ask that? It's not the same as this question, and in any case the answer probably requires more space than a comment allows. With that in mind, it'd be great if you could delete your previous comments here -- reduce the clutter. Thanks. – Caleb Feb 25 '14 at 14:59
10

See special File System Programming Guide for better understnading.
In iOS, you can't write into a file in your app's bundle -- the entire bundle is read-only.

Consider reading iOS Data Storage Guidelines to better understand the purpose of directories below, in context of iCloud backup.

<Application_Home>/AppName.app

This is the bundle directory containing the app itself. Do not write anything to this directory. To prevent tampering, the bundle directory is signed at installation time. Writing to this directory changes the signature and prevents your app from launching again.


<Application_Home>/Documents/

Use this directory to store critical user documents and app data files. Critical data is any data that cannot be recreated by your app, such as user-generated content. The contents of this directory can be made available to the user through file sharing. The contents of this directory are backed up by iTunes.


<Application_Home>/Library/

This directory is the top-level directory for files that are not user data files. You typically put files in one of several standard subdirectories but you can also create custom subdirectories for files you want backed up but not exposed to the user. You should not use this directory for user data files. The contents of this directory (with the exception of the Caches subdirectory) are backed up by iTunes. For additional information about the Library directory, see “The Library Directory Stores App-Specific Files.”

See full list (tmp/, Documents/Inbox) in iOS Standard Directories: Where Files Reside

UPDATE
I use NSFileManager method URLForDirectory:inDomain:appropriateForURL:create:error:

DanSkeel
  • 3,853
  • 35
  • 54
  • I read this full document even. I have confusion, if i have save my very imp file or videos in document folder. Then it will available to others easily. – python Feb 25 '14 at 13:53
  • 1
    You can't share files like this because every file is visible only from your app. To share docs you need [`Document Interaction Programming Guide`](https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40010403) – DanSkeel Feb 26 '14 at 10:33
8

Like Caleb said, you can't write to your app's directory, but you can write to your app's Documents folder. You can get it like this:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
iosfreak
  • 5,228
  • 11
  • 59
  • 102
6

Your app's bundle is read-only. There is two ways I could see:

1) Write in documents folder:

NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path =  [myPathList  objectAtIndex:0];

2) Use sqlite database. This is the same as 1 (you must save db in documents anyway), but you're using sqlite database. I think this is better than a lot of txt and plist files: here's a tutorial on the topic.

Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
HiTECNOLOGYs
  • 327
  • 5
  • 21
2

I use the following code :

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"set.txt"];

NSString *data=@"Kostas";

[data writeToFile:appFile atomically:YES];   

NSString *myData = [NSString stringWithContentsOfFile:appFile];   

NSLog(@"Data : %@ ",myData);
Kostas
  • 1,504
  • 1
  • 11
  • 13
  • 2
    What does this do differently from the code in the question? Why does the code in the question fail, and how does your code avoid that problem? – Peter Hosey Feb 10 '13 at 01:37
  • 2
    The differently is that I locate my file to Documents directory of the application, where I have access to write it. The code in the question uses app's bundle which is read-only. – Kostas Feb 10 '13 at 17:03