Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS: write a string in a file txt from an array

I have an array composed by other array:

example my array with two arrays inside:

myArray = [(element1, element2, element3)],[(element4,element5,element6)] this is only an example to show that myArray have two arrays (these elements are string)

now I want write in a txt file these elements in this way:

 element1#element2#element3;element4#element5#element6;

what is the code to create this string to write in txt file?

like image 576
cyclingIsBetter Avatar asked Jul 04 '11 09:07

cyclingIsBetter


1 Answers

    NSMutableString *printString = [NSMutableString stringWithString:@""];
    for(i=0;i<[myArray count];i++)
    {
        for (NSString element in [myArray objectAtIndex:i])
        {
            [printString appendString:[NSString stringWithFormat:@"%@#",element] ];
        }
        [printString appendString:@";"];
    }

    //CREATE FILE

    NSError *error;

    // Create file manager
    //NSFileManager *fileMgr = [NSFileManager defaultManager];

    NSString *documentsDirectory = [NSHomeDirectory() 
                                    stringByAppendingPathComponent:@"Documents"];

    NSString *filePath = [documentsDirectory 
                          stringByAppendingPathComponent:@"fileArray.txt"];

    NSLog(@"string to write:%@",printString);
    // Write to the file
    [printString writeToFile:filePath atomically:YES 
            encoding:NSUTF8StringEncoding error:&error];
like image 162
Dileep Reghu Avatar answered Oct 08 '22 04:10

Dileep Reghu