0

I need to delete the contents of a .txt file and I don't know how to delete the full text.

The file is in my mainBundle. I can access the file but I can't find the command to delete the text from inside it.

 NSFileManager *filemgr;        
    filemgr = [NSFileManager defaultManager];
    NSString* path = [[NSBundle mainBundle] pathForResource:@"UserData" 
                                                     ofType:@"txt"];


    NSString* content = [NSString stringWithContentsOfFile:path
                                                  encoding:NSUTF8StringEncoding
                                                     error:NULL];
DNA
  • 42,007
  • 12
  • 107
  • 146
Radu
  • 3,434
  • 4
  • 27
  • 38

5 Answers5

5

You can't. The apps mainBundle is readonly.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
  • could you give me an good alternative to save some text, string data in my app? – Radu Feb 17 '12 at 13:15
  • 1
    @Radu Either use NSUserDefaults (https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html) or save to a writable place (http://stackoverflow.com/a/1567147/477878) – Joachim Isaksson Feb 17 '12 at 13:41
1

try creating a new file with the same name if it is not ready- only file ..

tharani dharan
  • 425
  • 3
  • 8
  • 18
1

You shouldn't do this on iOS in production apps. The bundle is signed, so changing its contents will make the app stop working.

mvds
  • 45,755
  • 8
  • 102
  • 111
1

The files in the mainBundle are not writable with a non jailbroken iPhone, so erasing the contents of the file in its current location cannot be done.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
0

The proper way is to make a copy of your text file from the app bundle into the user's Documents folder and make whatever changes you want there. Preferably, you should do this the first time your user launches your app.

The contents of the app bundle is read-only and should never be modified.

XCool
  • 976
  • 11
  • 32