From 44fc375416ea3690416f17cb5f1433c08e939cb3 Mon Sep 17 00:00:00 2001 From: Xiao Pan Date: Wed, 7 May 2025 19:35:14 -0700 Subject: consider large section of bad data --- mycan.c | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/mycan.c b/mycan.c index 7fdef0c..59e49b2 100644 --- a/mycan.c +++ b/mycan.c @@ -87,31 +87,30 @@ int main(int argc, char **argv) //printf("this is a can receive demo\r\n"); // check and fix corrupted .csv file + for(FILE *fp;(fp=fopen(filename,"r"))!=NULL;) { - FILE * fp; - if((fp=fopen(filename,"r"))!=NULL) + 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) { - 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); - } + 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); } + else + break; } //1.Create socket -- cgit v1.2.3-70-g09d2