0

I am trying to write a string to a t.t file in the documents folder of my app. I can write the string to it, but when I write another string to the file it overwrites the other string, is it possible to write more strings to a text files, with a blank line between strings, many in this formate

String

String

Strine

I am using this code to write the string to an text file, it works for one string, but not for multible strings.

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:@"%@/Barcodedata.txt",
                              documentsDirectory];
        //create content - four lines of text
        NSString *content = [NSString stringWithFormat:@"%@",sym.data];
        //save content to the documents directory
        [content writeToFile:fileName
                  atomically:NO
                    encoding:NSStringEncodingConversionAllowLossy
                       error:nil];
David Gölzhäuser
  • 3,525
  • 8
  • 50
  • 98
  • Yes, it is possible. What have you tried? –  Apr 06 '13 at 09:51
  • 1
    You can use `\r\n` to specify a line break. Check out my answer [here](http://stackoverflow.com/questions/5905891/read-and-write-an-integer-to-from-a-txt-file/15958524#15958524) for details. – GeneralMike Apr 11 '13 at 20:51

1 Answers1

2

There's a few ways to do this, depending on how you implemented your code.

One way would be to load up the original .txt file into a NSMutableString object and then append that new line to the end of the string and re-write out the file (this isn't super efficient, especially as you start appending after 1000 strings, 100 strings, 50 strings, etc.)

Or you could use the low level C function "fwrite" with the append bit set.

Edited:

Since you want to see code, here's how to do it with my first suggestion:

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:@"%@/Barcodedata.txt", documentsDirectory];
//create content - four lines of text

NSError * error = NULL;
NSStringEncoding encoding;
NSMutableString * content = [[NSMutableString alloc] initWithContentsOfFile: fileName usedEncoding: &encoding error: &error];
if(content == NULL)
{
    // if the file doesn't exist yet, we create a mutable string
    content = [[NSMutableString alloc] init];
}

if(content)
{
    [content appendFormat: @"%@", sym.data];

    //save content to the documents directory
    BOOL success = [content writeToFile:fileName
                            atomically:NO
                              encoding:NSStringEncodingConversionAllowLossy
                                 error:&error];

    if(success == NO)
    {
        NSLog( @"couldn't write out file to %@, error is %@", fileName, [error localizedDescription]);
    }
}
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • so why not turn "`content`" into a "`NSMutableString`", load it via [`[[NSMutableString alloc] initWithContentsOfFile:usedEncoding:error:]`](http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/initWithContentsOfFile:usedEncoding:error:) and then append your new stuff onto the end of "`content`" before writing it out? – Michael Dautermann Apr 06 '13 at 10:01
  • What!? Can you please provide me an example. – David Gölzhäuser Apr 06 '13 at 10:09