diff options
author | Xiao Pan <xyz@flylightning.xyz> | 2025-05-07 18:40:25 -0700 |
---|---|---|
committer | Xiao Pan <xyz@flylightning.xyz> | 2025-05-07 18:40:25 -0700 |
commit | c04eb006f3f04975964f9ede06d3371a565a7ea6 (patch) | |
tree | e9247a9bb5a28f05468ad13db462a95c5b81fd7b | |
parent | 34eb39da57c7ebb6c32591960e1cb111cc8de7cb (diff) |
Only fix if file exist so it can be opened
-rw-r--r-- | mycan.c | 44 |
1 files changed, 21 insertions, 23 deletions
@@ -89,30 +89,28 @@ int main(int argc, char **argv) // check and fix corrupted .csv file { FILE * fp; - char ch; - uint32_t offset; - int remain; - if((fp=fopen(filename,"r"))==NULL) + if((fp=fopen(filename,"r"))!=NULL) { - fprintf(stderr,"Can't open file \"%s\".\n",filename); - exit(1); - } - fseek(fp,-1L,SEEK_END); - fscanf(fp,"%c",&ch); - fseek(fp,0L,SEEK_END); - offset=ftell(fp); - printf("csv file end offset: %d\n",offset); - fclose(fp); - remain=offset%ENTRY_SIZE; - if(remain) - { - printf("Corrupted .csv file not a multiple of %d, removing last line\n",ENTRY_SIZE); - truncate(filename,offset-remain); - } - else if(ch != '\n') - { - printf("Corrupted .csv file last char not newline, removing last line\n"); - truncate(filename,offset-ENTRY_SIZE); + char ch; + uint32_t offset; + int remain; + fseek(fp,-1L,SEEK_END); + fscanf(fp,"%c",&ch); + fseek(fp,0L,SEEK_END); + offset=ftell(fp); + //printf("csv file end offset: %d\n",offset); + fclose(fp); + remain=offset%ENTRY_SIZE; + if(remain) + { + printf("Corrupted .csv file not a multiple of %d, removing last line\n",ENTRY_SIZE); + truncate(filename,offset-remain); + } + else if(ch != '\n') + { + printf("Corrupted .csv file last char not newline, removing last line\n"); + truncate(filename,offset-ENTRY_SIZE); + } } } |