I have a text file created in documents directory with 200 logs/lines (with delimiter to separate data using a string). I want to TRANSFER first 50 records into another file and keep the remaining 150 records in the same file.
I thought of a logic, say
NSString *logString = [[NSString alloc]initWithContentsOfFile:logFilePath encoding:NSUTF8StringEncoding error:nil];
NSArray *logsArray = [logString componentsSeparatedByString:@"[;]"];
[@"" writeToFile:logFilePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
for (int i=0; i<=[logsArray count];i++){
if(i<=50){
"write to new file"
}
else{
"write to old file"
}
}
This logic will work fine. My concern is I am at a heavy risk of losing my data since I am emptying and rewriting my file at one point. I am already creating too much of files to back up my data. Is there any solution to handle this case ?
Thanks - Arun.AR
This way, you always keep around the original file just until the end. If something happens (crash etc.) you always have all the lines around. They could be in different files, but you can always get them back later with a little more logic.
Actually, the writing of the last 150 objects can be done atomically by the Cocoa framework for you, but these steps are the gist of it.