Reputation: 1233
What I want to do is write a method that when called will create a plain text file with a pre-written header (always the same) and then periodically updates the text file with more data until the user requests it to stop. A new text file would be needed for each different time the user uses the app.
I'm having difficulty even getting the app to create the text file in the first place. Any suggestions what I may need to do to accomplish this?
Thanks.
Upvotes: 1
Views: 662
Reputation: 1905
Have a look on following code, it creates a CSV file. Which is exactly what you require. If file doesn't exist it creates a new one and write headers first, otherwise just write the log text.
- (void)log:(NSString *)msg {
NSString *fileName = [self logFilePath];
// if new file the add headers
FILE *file = fopen([fileName UTF8String], "r");
if (file == NULL) {
file = fopen([fileName UTF8String], "at");
fprintf(file, "%s\n", "Date, Time, Latitude, Longitude, Speed, info");
} else {
fclose(file);
file = fopen([fileName UTF8String], "at");
}
fprintf(file, "%s\n", [msg UTF8String]);
fclose(file);
}
You should create your files in document directory, following code shows how to get path to the document directory
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
Upvotes: 1