Reputation: 381
I have a .txt
file with a small password in it. I would like to change that file's text. How can I do that? I can get the file's text using :
NSString *passWord = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"password" ofType:@"txt"] encoding:NSUTF8StringEncoding error:NULL];
But I can't seem to find a way to replace the contents of the file with a string.
Upvotes: 0
Views: 70
Reputation: 559
The way to do this is to remove old file and create new one. Removing existing file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myFile.txt"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:filePath])
{
[fileManager removeItemAtPath:filePath error:&error];
}
To crate new file with your new password write password data while creating file as:
[createFileAtPath:path contents:[NSData data] attributes:nil]
Upvotes: 1