Question

I'm having a big trouble. I'd like to concatenate data of multiple textfiles into another textfiles. But I cannot. can you help me ? Many thanks

Was it helpful?

Solution

Read each file one by one,

NSString *firstFileContent = [NSString stringWithContentsOfFile:<your file path>
                                                       encoding:NSASCIIStringEncoding
                                                          error:nil];

//Similarly read other files, and store them in secondFileContent and thirdFileContent.

//now concatenate all to form one big string.
NSString *bigString = [NSString stringWithFormat:@"-First File- \n%@ \n-Second File- \n%@\n-Third File-\n%@",firstFileContent, secondFileContent, thirdFileContent];

//write to file, create a new one
[bigString writeToFile:<path to write>
            atomically:YES
              encoding:NSASCIIStringEncoding
                 error:nil];

Edit 1 :

As per your comment that your file is in DocumentDirectory use this code :

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:<your file name>];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

OTHER TIPS

First load content of file in NSString and use following code:

   NSString *strConcatenate = [NSString stringWithFormat:@"%@ %@ %@", textfiles1, textfiles2, textfiles3];    
   NSLog(@"%@", strConcatenate);

You just have to load the content of the files into NSMutableString and concatenate them :

NSMutableString *myString = @"Content of the first file";
NSString *test = [myString stringByAppendingString:@" content of the second file"];

You need to read the text files in from the bundle and then append them, once you have that then write it back out. I wrote this example and I hope you can learn from it.

NSMutableString *mutableString = [[NSMutableString alloc] init];

NSArray *textFiles = @[ @"textfile1", @"textfile2", @"textfile3" ];

for (NSString *textFileName in textFiles) {

  NSString *path = [[NSBundle mainBundle] pathForResource:textFileName 
                                                   ofType:@"txt"];

  NSError *error = nil;
  NSString *content = [NSString stringWithContentsOfFile:path
                                                encoding:NSUTF8StringEncoding
                                                   error:&error];

  if (content) {
    [mutableString appendFormat:@"%@\n", content];
  } else {
    NSLog(@"%@", error.localizedDescription);
  }

}

NSLog(@"%@", mutableString);

NSError *error = nil;
BOOL result = [mutableString writeToFile:@"concatenated_file.txt" atomically:NO encoding:NSStringEncodingConversionAllowLossy error:&error];

if (!result) {
  NSLog(@"%@", error.localizedDescription);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top