c - Converting text to a binary file -
i need convert text file of following format binary:
the first line contains number of products in inventory, following lines contains:
product name '\t'
product price '\t'
quantity '\n'
(there can more 1 \t between columns)
for every product binary output file contain int representing length of product name, chars hold product name, int representing price , int representing quantity.
sample input file:
asus zenbook 1000 10 iphone 5 750 22 playstation 4 1000 0
i have wrote following code, , understood i'm supposed see string in plain text while integers show gibberish (in binary):
int converttexttobinary(char *filename) { file *ptext, *pbinary; int size, i; char *currprodname; int currprodnamelen, currquantity, currprice; if (checkfileexists(filename) == false) { printf("- given file not exists!\n"); return error; } else ptext = fopen(filename, "r"); // number of products in inventory fscanf(ptext, "%d", &size); #ifdef dbg printf("##dbg successfuly read &size = %d dbg##\n", size); #endif pbinary = fopen(strcat(filename, ".bin"), "wb"); fwrite(&size, sizeof(int), 1, pbinary); #ifdef dbg printf("##dbg successfuly wrote &size = %d dbg##\n", size); #endif (i = 0; < size; i++) { // product name , name length currprodnamelen = getprodname(ptext, &currprodname); #ifdef dbg printf("##dbg %d successfuly read &currprodname = %s dbg##\n", i+1, currprodname); printf("##dbg %d successfuly read &currprodnamelen = %d dbg##\n", i+1, currprodnamelen); #endif // product price fscanf(ptext, "%d", &currprice); printf("##dbg %d successfuly read &currprice = %d dbg##\n", i+1, currprice); // product quantity fscanf(ptext, "%d", &currquantity); printf("##dbg %d successfuly read &currquantity = %d dbg##\n", i+1, currquantity); // write data binary file fwrite(&currprodnamelen , sizeof(int), 1, pbinary); fwrite(&currprodname, sizeof(char), currprodnamelen, pbinary); fwrite(&currprice, sizeof(int), 1, pbinary); fwrite(&currquantity, sizeof(int), 1, pbinary); free(currprodname); } fclose(ptext); fclose(pbinary); return 1; } /* function checks if file in given path exists or not using fopen "read" argument */ bool checkfileexists(char *filename) { file *fp; fp = fopen(filename, "r"); // file not exists if (fp == null) return false; // file exists else { fclose(fp); return true; } } int getprodname(file *fp, char **prodname) { int namelen = 0, offset; // count length of product name while (fgetc(fp) != '\t') namelen++; // allcoate memory product name *prodname = (char*)malloc(sizeof(char)*namelen); //checkalloc(&prodname); // cursor original position offset = -1 * namelen; fseek(fp, offset, seek_cur); // copy product name text string fgets(*prodname, namelen, fp); return strlen(*prodname); }
but hell, output file looks this:
¨ ּּּּּט ¨ ּּּ¯ ¨ ּּּּּּּּ ּּּ« ¨
which holds no plain text. have tried changing fopen argument "wb" "w" still gibberish files. doing wrong?
here write pointer , additional garbage instead of string points to:
fwrite(&currprodname, sizeof(char), currprodnamelen, pbinary);
you should use:
fwrite(currprodname, sizeof(char), currprodnamelen, pbinary);
in version passing pointer pointer, want pass pointer itself.
btw: in function getprodname()
, should add additional character, because allocating exact string lenght, no room 0
byte @ end. can cause problems. fgets
reads 1 char less. check man page fgets
. instead of using fgets
, can use fread
because know length anyway. no additional parsing needed.
update
change this:
fscanf(ptext, "%d", &currquantity);
to
fscanf(ptext, "%d\n", &currquantity);