Hi,
I'm writing CVS formatted data to a log file. When the system loses power unexpectedly and the write operation is interrupted it can happen that the last line of the file is incomplete (fewer columns). To tidy up the file I want to remove the last line. Currently the only option to do this seems to copy the entire file, which is quiet inefficient (files can have 100MB to 1GB). On a unix system one could use the ftruncate function to remove the tail of the file. Is there a similar (efficient) option for the Keil filesystem?
Best,
Valentin
Just a suggestion: You could set the in-file pointer to the end of the last complete line with the function fseek() and then you could continue to write correct lines. This is supported for the FAT file system, but not for not for the EFS (write mode).
Thanks, I'll try that.
I think you're looking at the wrong end of this problem.
Rather than burdening your embedded system with this job, it's almost ceraintly more productive to leave it to post-processing steps on whatever system you eventually shunt that log file over to. There you'll have no problem whatsoever setting up a little script that cleans up this kind of gllitch before processing the file for real.
Just make sure you issue a line-end if you find the last line to be lacking one, on boot-up.
I've tried this, but I can't get it to work. Here is a minimal example:
FILE* f = fopen(file_name, "rw"); if (f == NULL) return -1; int rc = fseek(f, 0x388, SEEK_SET); // 0x388 is smaller than the file size. if (rc != 0) { return -1; } rc = fprintf(f, "hello world"); if (rc <= 0) { // rc is always equal to -1 return -1; }
I'm using ARM Compiler 5 and MDK-Middleware 7.17.3.
This was my first thought too, but I'm relying on a third party PC tool that (unfortunately) doesn't handle corrupted lines. Since Hans Schneebauer's solution would be quiet easy I'd like to try that.
And you absolutely have to let that immutable tool have direct access to the file on your device? There's no way you can let it operate on a filtered copy of the log?
If I can't get the above solution to work, this would probable be the to go solution.
The open mode "rw" could be the issue, which does not exist. Please try with "r+" (read update mode).
Thank you, this has fixed the problem.