aboutsummaryrefslogtreecommitdiff
path: root/mycan.c
diff options
context:
space:
mode:
authorXiao Pan <xyz@flylightning.xyz>2025-05-07 19:35:14 -0700
committerXiao Pan <xyz@flylightning.xyz>2025-05-07 19:35:14 -0700
commit44fc375416ea3690416f17cb5f1433c08e939cb3 (patch)
tree9b84543899ccb9f52a10a24c161b5207953c9a83 /mycan.c
parentc04eb006f3f04975964f9ede06d3371a565a7ea6 (diff)
consider large section of bad data
Diffstat (limited to 'mycan.c')
-rw-r--r--mycan.c43
1 files 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