Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C creating a text file with a string

Tags:

I'm trying to create a text file with the contents of a string to my desktop. I'm not sure if I'm doing it right, I don't get errors but it doesn't work either...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES); NSString *desktopDirectory=[paths objectAtIndex:0]; NSString *filename = [desktopDirectory stringByAppendingString: @"file.txt"]; [myString writeToFile:filename atomically:YES encoding: NSUTF8StringEncoding error: NULL]; 
like image 925
ACV Avatar asked Nov 30 '09 14:11

ACV


1 Answers

//Method writes a string to a text file -(void) writeToTextFile{     //get the documents directory:     NSArray *paths = NSSearchPathForDirectoriesInDomains         (NSDocumentDirectory, NSUserDomainMask, YES);     NSString *documentsDirectory = [paths objectAtIndex:0];      //make a file name to write the data to using the documents directory:     NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt",                                                    documentsDirectory];     //create content - four lines of text     NSString *content = @"One\nTwo\nThree\nFour\nFive";     //save content to the documents directory     [content writeToFile:fileName                       atomically:NO                             encoding:NSStringEncodingConversionAllowLossy                                    error:nil];  } 
like image 97
megha Avatar answered Sep 26 '22 00:09

megha