i got an assigment, for tomorow, i have a text file, and i need to convert it to binary file.
text file for example:
2 - how many products
ipod 1000 2 - product name, price stock
playStation 23 9
i need to preper this binary file:
first the size, the lengh of the product name, the product name,
this is my code..
void ConverToBinary(FILE* fp,FILE* newFp)
{
int size;
fseek(fp,0,SEEK_SET);
fscanf(fp,"%d\n",&size);
//write the size
fwrite(&size,sizeof(int),1,newFp);
ConvertStock(fp,newFp,size);
}
void ConvertStock(FILE* fp,FILE* newFp,int size)
{
int len=0,i=0,flag=TRUE,cost,stock,y1,y2;
char ch,*prod;
long int location,locationLen;
char x[256];
locationLen = ftell(newFp);
// fwrite(&i,sizeof(int),1,newFp);
while (flag)
{
ch = fgetc(fp);
if (ch == EMPTY)
flag = FALSE;
else if (ch =='\t')
{
fseek(newFp,locationLen,SEEK_SET);
fwrite(&len,sizeof(int),1,newFp);
fseek(newFp,location,SEEK_SET);
fscanf(fp,"%d",&cost);
fscanf(fp,"%d\n",&stock);
fwrite(&cost,sizeof(int),1,newFp);
fwrite(&stock,sizeof(int),1,newFp);
len = 0 ;
locationLen = ftell(newFp);
// fwrite(&i,sizeof(int),1,newFp);
}
else
{
len++;
fwrite(&ch,sizeof(char),1,newFp);
location = ftell(fp);
}
}
the thing is,
when i reading from it, its really a mess.
i am trying to move back and forward and probably it messes it up