Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bin/csv2bin
Binary file not shown.
6 changes: 6 additions & 0 deletions inc/csv2bin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 52 additions & 0 deletions src/csv2bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 5 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Binary file modified test/ts.bin
Binary file not shown.