diff --git a/bin/csv2bin b/bin/csv2bin index c2842ef..40ba14c 100755 Binary files a/bin/csv2bin and b/bin/csv2bin differ diff --git a/inc/csv2bin.h b/inc/csv2bin.h index 88f2f25..cfb9dae 100644 --- a/inc/csv2bin.h +++ b/inc/csv2bin.h @@ -50,4 +50,10 @@ Read and display bin file with tserie */ int readBINFile(tfile *tsf); +/* +Function to save a txt file into binary format. +*/ +int txtIntoBin(tfile *ftxt, tfile *fbin); + + #endif diff --git a/src/csv2bin.c b/src/csv2bin.c index 00cbc24..8c04ee7 100644 --- a/src/csv2bin.c +++ b/src/csv2bin.c @@ -216,4 +216,56 @@ int readBINFile(tfile *tsf){ return 0; } +/* +Function to save a txt file into binary format. +*/ +int txtIntoBin(tfile *ftxt, tfile *fbin){ + + FILE *ftx, *fbi; + char line[MAXCHAR]; + char *ftxname, *fbiname, *linebi; + int lline; + + /* Make up txt filename */ + ftxname = (char *)malloc(strlen(ftxt->dirname) + strlen(ftxt->filename)); + strcpy(ftxname, ftxt->dirname); + strcat(ftxname, ftxt->filename); + + /* Make up bin filename */ + fbiname = (char *)malloc(strlen(fbin->dirname) + strlen(fbin->filename)); + strcpy(fbiname, fbin->dirname); + strcat(fbiname, fbin->filename); + + /* Open txt file */ + if ((ftx = fopen(ftxname,"r")) == NULL){ + printf("Error opening %s txt file, stopping... \n", ftxname); + return -1; + } + + /* Open bin file */ + if ((fbi = fopen(fbiname,"wb")) == NULL){ + printf("Error opening %s bin file, stopping... \n", fbiname); + return -1; + } + + /* Read the txt file and write into bin */ + while (fgets(line, MAXCHAR, ftx) != NULL){ + + lline = strlen(line); + printf("le = %d\n",lline); + linebi = (char *)malloc(lline * sizeof(char)); + strcpy(linebi, line); + + fwrite(linebi, lline*sizeof(char), 1, fbi ); + + free(linebi); + } + + fclose(ftx); + fclose(fbi); + free(ftxname); + free(fbiname); + + return 0; +} diff --git a/src/main.c b/src/main.c index 4f9b9ba..7555fb6 100644 --- a/src/main.c +++ b/src/main.c @@ -56,13 +56,15 @@ int main(int argc, char *argv[]) #endif #endif - /* Freeing heap memory */ - /*freeMemCSV(&ts);*/ - /* Allocate header array */ + /* Free allocated array */ + free(data); for(i = 0; i < NHEADER; i++){ free(header[i]); } + + + txtIntoBin(&tsf, &tsfb); return 0; } diff --git a/test/ts.bin b/test/ts.bin index bb0e90b..4faaf3e 100644 Binary files a/test/ts.bin and b/test/ts.bin differ