Created
June 9, 2017 10:03
-
-
Save iyer-arvind/ac7701711aaaacaac3989dc3a75e5247 to your computer and use it in GitHub Desktop.
Convert CSV to Binary converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <math.h> | |
typedef struct { | |
double t; | |
double x; | |
double y; | |
double z; | |
int prank; | |
char etype[5]; | |
int uidx; | |
int eidx; | |
double data[5]; | |
} data_t; | |
int main (int argc, char **argv) | |
{ | |
if(argc<3) | |
{ | |
printf("Usage %s <inp-file> <out-file>\n",argv[0]); | |
exit(1); | |
} | |
FILE *fin=fopen(argv[1],"r"); | |
FILE *fout=fopen(argv[2],"wb"); | |
char *line=NULL; | |
data_t data; | |
memset(&data,0,sizeof(data_t)); | |
double old_t=-1; | |
while(!feof(fin)) | |
{ | |
long unsigned int count; | |
int nf_ref=-1, nf=0; | |
getline(&line, &count, fin); | |
for(int i=0; i<count; i++)if(line[i]==',') nf++; | |
if(nf_ref>0) | |
{ | |
if(nf!=nf_ref) | |
{ | |
printf("SKIPPING\n"); | |
continue; | |
} | |
} | |
else | |
{ | |
nf_ref = nf; | |
} | |
fflush(stdout); | |
sscanf(line,"%lf,%lf,%lf,%lf,%d,%4[^,],%d,%d,%lf,%lf,%lf,%lf,%lf", | |
&data.t, &data.x, &data.y, &data.z, | |
&data.prank, &data.etype, &data.uidx, &data.eidx, | |
&data.data[0], &data.data[1], &data.data[2], &data.data[3], &data.data[4]); | |
fwrite(&data,sizeof(data),1,fout); | |
if(fabs(old_t-data.t)>1e-10) | |
{ | |
printf("%.11lf\n",data.t); | |
old_t = data.t; | |
fflush(stdout); | |
} | |
} | |
if(line) | |
free(line); | |
fclose(fin); | |
fclose(fout); | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment