Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear contents of file

I am trying to remove the contents text file for iOS.

Alternatively, I could delete the file then re-create it right after with no contents.

For this solution, I know I can delete the file using [fileManager removeItemAtPath:filePath error:&error];. Recreating the file is the hard part for me.

I am locating the file just fine using the following code:

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];
}

//code to recreate the same file, just with no content
like image 921
Johnrad Avatar asked Feb 16 '23 16:02

Johnrad


1 Answers

Try

-[[NSFileManager defaultManager] createFileAtPath:path contents:[NSData data] attributes:nil]
like image 149
maroux Avatar answered Feb 27 '23 19:02

maroux