0
votes

I want to modify (Add a new row) existing csv file in xcode. i have created a csv file programmatically , but when i try to modify it , it delete all the previos data.

i am opening an existing csv file and want to add a next row.

NSString *docFile = [DOCS_FOLDER stringByAppendingPathComponent:@"Questions_Logs.csv"]; NSFileManager* filemanager = [NSFileManager defaultManager];

//---get the contents of file and if content avalbl than append new data and save it--

if ([filemanager fileExistsAtPath:docFile]) 

{

 NSArray *arrCSV = [NSArray arrayWithObjects:strName,strAge,strDesig, nil];
 [[arrCSV componentsJoinedByString:@","] writeToFile:docFile atomically:YES encoding:NSUTF8StringEncoding error:NULL];

}

else

{ NSArray *arrCSV = [NSArray arrayWithObjects:@"Name",@"Age",@"Designation", nil]; [[arrCSV componentsJoinedByString:@","] writeToFile:docFile atomically:YES encoding:NSUTF8StringEncoding error:NULL]; }

1
We probably need to see some code. A guess, though: Are you opening the file for writing as opposed to appending? - hdgarrood
You haven't said which language you're using, but when opening files, you usually have to specify a mode. Look at the 'mode' section in cplusplus.com/reference/cstdio/fopen - hdgarrood
what in your question has anything to do with Xcode? - John

1 Answers