objective c - How to write a String in different lines to a .txt file on iOS -


i trying write string t.t file in documents folder of app. can write string it, when write string file overwrites other string, possible write more strings text files, blank line between strings, many in formate

string

string

strine

i using code write string text file, works 1 string, not multible strings.

nsarray *paths = nssearchpathfordirectoriesindomains         (nsdocumentdirectory, nsuserdomainmask, yes);         nsstring *documentsdirectory = [paths objectatindex:0];          //make file name write data using documents directory:         nsstring *filename = [nsstring stringwithformat:@"%@/barcodedata.txt",                               documentsdirectory];         //create content - 4 lines of text         nsstring *content = [nsstring stringwithformat:@"%@",sym.data];         //save content documents directory         [content writetofile:filename                   atomically:no                     encoding:nsstringencodingconversionallowlossy                        error:nil]; 

there's few ways this, depending on how implemented code.

one way load original .txt file nsmutablestring object , append new line end of string , re-write out file (this isn't super efficient, start appending after 1000 strings, 100 strings, 50 strings, etc.)

or use low level c function "fwrite" append bit set.

edited:

since want see code, here's how first suggestion:

nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0];  //make file name write data using documents directory: nsstring *filename = [nsstring stringwithformat:@"%@/barcodedata.txt", documentsdirectory]; //create content - 4 lines of text  nserror * error = null; nsstringencoding encoding; nsmutablestring * content = [[nsmutablestring alloc] initwithcontentsoffile: filename usedencoding: &encoding error: &error]; if(content == null) {     // if file doesn't exist yet, create mutable string     content = [[nsmutablestring alloc] init]; }  if(content) {     [content appendformat: @"%@", sym.data];      //save content documents directory     bool success = [content writetofile:filename                             atomically:no                               encoding:nsstringencodingconversionallowlossy                                  error:&error];      if(success == no)     {         nslog( @"couldn't write out file %@, error %@", filename, [error localizeddescription]);     } } 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -