happogiri
October 31st, 2007, 09:21 PM
Once again I'm knee deep in...something.
I'm sorry for the not-so-illustrating translations of my variables and such.
/* For example: "Wheatflour 3 cups" */
typedef struct ingredient {
char *name;
int nSize; /* Length of name */
float amount;
char *unit;
int uSize; /* Length of unit */
} Ingredient;
typedef struct recipe {
char *name; /* For example "Pizza Bolognese" */
int nSize;
Ingredient **ingredients;
int iAmount; /* How many ingredients */
char *howto; /* Howto cook */
int hSize; /* Length of howto */
} Recipe;
typedef struct list {
Recipe *recipe;
struct list *next;
} Cookbook;
Here are my three structures. This is supposed to be a cookbook, collection of recipes. Everytime the program starts it reads the recipes from binary file to linked list and when it shuts it writes them to file (you can browse, add, remove, edit, etc...). Anyways, my problem is that I can't figure it out how to write and read that certain float-amount in Integer to (and from) binary file.
Here is how I write them in file:
void writeRecipesToFile(char *fname, Cookbook *head) {
FILE *tmpFile;
Cookbook *tmp;
int strSize;
int i;
tmp = head;
if((tmpFile = fopen(fname, "wb")) == NULL) {
printf("Error...!");
exit(EXIT_FAILURE);
}
while(tmp != NULL) {
fwrite(&(tmp->recipe->nSize), sizeof(int), 1, tmpFile);
fwrite(tmp->recipe->name, tmp->recipe->nSize, 1, tmpFile);
fwrite(&(tmp->recipe->iAmount), sizeof(int), 1, tmpFile);
for(i = 0; i < tmp->recipe->iAmount; i++) {
fwrite(&(tmp->recipe->ingredients[i]->nSize), sizeof(int), 1, tmpFile);
fwrite(tmp->recipe->ingredients[i]->name, tmp->recipe->ingredients[i]->nSize, 1, tmpFile);
fwrite(&(tmp->recipe->ingredients[i]->amount), sizeof(float), 1, tmpFile);
fwrite(&(tmp->recipe->ingredients[i]->uSize), sizeof(int), 1, tmpFile);
fwrite(tmp->recipe->ingredients[i]->unit, tmp->recipe->ingredients[i]->uSize, 1, tmpFile);
}
fwrite(&(tmp->recipe->hSize), sizeof(int), 1, tmpFile);
fwrite(tmp->recipe->howto, tmp->recipe->hSize, 1, tmpFile);
tmp = tmp->next;
}
fclose(tmpFile);
}
Is there any sense in it?
The reading is kind of similar but with fread().
void readRecipesFromFile(char *fname, Cookbook **head) {
FILE *tmpFile;
Recipe *tmpRecipe;
char *rname, *rhowto;
int rnSize, rhSize;
int iam;
Ingredient **tmpIng;
char *iname, *iunit;
int inSize, iuSize;
float amount;
int i;
if((tmpFile = fopen(fname, "rb")) == NULL) {
printf("Error!");
exit(EXIT_FAILURE);
}
LOOP UNTIL END OF FILE {
fread(&rnSize, sizeof(int), 1, tmpFile);
if((rname = malloc(rnSize)) == NULL) {
exit(EXIT_FAILURE);
}
fread(rname, rnSize, 1, tmpFile);
fread(&iam, sizeof(int), 1, tmpFile);
for(i = 0; i < iam; i++) {
fread(&inSize, sizeof(int), 1, tmpFile);
if((iname = malloc(rnSize)) == NULL) {
exit(EXIT_FAILURE);
}
fread(iname, rnSize, 1, tmpFile);
if(fread(&amount, sizeof(float), 1, tmpFile) != 1) {
printf("Error!");
exit(EXIT_FAILURE);
}
fread(&iuSize, sizeof(int), 1, tmpFile);
if((iunit = malloc(rnSize)) == NULL) {
exit(EXIT_FAILURE);
}
fread(iunit, iuSize, 1, tmpFile);
tmpIng[i] = createIngredient(iname, amount, iunit);
free(iname);
free(iunit);
}
fread(&rhSize, sizeof(int), 1, tmpFile);
fread(rhowto, rhSize, 1, tmpFile);
createRecipe(rname, rhowto, tmpIng, iam, &tmpRecipe);
void addRecipeToList(head, tmpReciper);
free(rname);
free(rhowto);
} END LOOP
fclose(tmpFile);
}
The writing seems to work so and so, but when I print the values I've read, the "float amount" -value in Ingredient is something like: 686098745690300000.00000 and after that everything else is garbage too. When I write the file as normal text-file everything seems to be there (all the numbers, int and float, are "binary" but text is there). So, where did I go wrong (please, be gentle)?
I will check the return values of each function ( if(fread(....) != 1) dosomething...) when I know I can make it work. :)
I'm sorry for the not-so-illustrating translations of my variables and such.
/* For example: "Wheatflour 3 cups" */
typedef struct ingredient {
char *name;
int nSize; /* Length of name */
float amount;
char *unit;
int uSize; /* Length of unit */
} Ingredient;
typedef struct recipe {
char *name; /* For example "Pizza Bolognese" */
int nSize;
Ingredient **ingredients;
int iAmount; /* How many ingredients */
char *howto; /* Howto cook */
int hSize; /* Length of howto */
} Recipe;
typedef struct list {
Recipe *recipe;
struct list *next;
} Cookbook;
Here are my three structures. This is supposed to be a cookbook, collection of recipes. Everytime the program starts it reads the recipes from binary file to linked list and when it shuts it writes them to file (you can browse, add, remove, edit, etc...). Anyways, my problem is that I can't figure it out how to write and read that certain float-amount in Integer to (and from) binary file.
Here is how I write them in file:
void writeRecipesToFile(char *fname, Cookbook *head) {
FILE *tmpFile;
Cookbook *tmp;
int strSize;
int i;
tmp = head;
if((tmpFile = fopen(fname, "wb")) == NULL) {
printf("Error...!");
exit(EXIT_FAILURE);
}
while(tmp != NULL) {
fwrite(&(tmp->recipe->nSize), sizeof(int), 1, tmpFile);
fwrite(tmp->recipe->name, tmp->recipe->nSize, 1, tmpFile);
fwrite(&(tmp->recipe->iAmount), sizeof(int), 1, tmpFile);
for(i = 0; i < tmp->recipe->iAmount; i++) {
fwrite(&(tmp->recipe->ingredients[i]->nSize), sizeof(int), 1, tmpFile);
fwrite(tmp->recipe->ingredients[i]->name, tmp->recipe->ingredients[i]->nSize, 1, tmpFile);
fwrite(&(tmp->recipe->ingredients[i]->amount), sizeof(float), 1, tmpFile);
fwrite(&(tmp->recipe->ingredients[i]->uSize), sizeof(int), 1, tmpFile);
fwrite(tmp->recipe->ingredients[i]->unit, tmp->recipe->ingredients[i]->uSize, 1, tmpFile);
}
fwrite(&(tmp->recipe->hSize), sizeof(int), 1, tmpFile);
fwrite(tmp->recipe->howto, tmp->recipe->hSize, 1, tmpFile);
tmp = tmp->next;
}
fclose(tmpFile);
}
Is there any sense in it?
The reading is kind of similar but with fread().
void readRecipesFromFile(char *fname, Cookbook **head) {
FILE *tmpFile;
Recipe *tmpRecipe;
char *rname, *rhowto;
int rnSize, rhSize;
int iam;
Ingredient **tmpIng;
char *iname, *iunit;
int inSize, iuSize;
float amount;
int i;
if((tmpFile = fopen(fname, "rb")) == NULL) {
printf("Error!");
exit(EXIT_FAILURE);
}
LOOP UNTIL END OF FILE {
fread(&rnSize, sizeof(int), 1, tmpFile);
if((rname = malloc(rnSize)) == NULL) {
exit(EXIT_FAILURE);
}
fread(rname, rnSize, 1, tmpFile);
fread(&iam, sizeof(int), 1, tmpFile);
for(i = 0; i < iam; i++) {
fread(&inSize, sizeof(int), 1, tmpFile);
if((iname = malloc(rnSize)) == NULL) {
exit(EXIT_FAILURE);
}
fread(iname, rnSize, 1, tmpFile);
if(fread(&amount, sizeof(float), 1, tmpFile) != 1) {
printf("Error!");
exit(EXIT_FAILURE);
}
fread(&iuSize, sizeof(int), 1, tmpFile);
if((iunit = malloc(rnSize)) == NULL) {
exit(EXIT_FAILURE);
}
fread(iunit, iuSize, 1, tmpFile);
tmpIng[i] = createIngredient(iname, amount, iunit);
free(iname);
free(iunit);
}
fread(&rhSize, sizeof(int), 1, tmpFile);
fread(rhowto, rhSize, 1, tmpFile);
createRecipe(rname, rhowto, tmpIng, iam, &tmpRecipe);
void addRecipeToList(head, tmpReciper);
free(rname);
free(rhowto);
} END LOOP
fclose(tmpFile);
}
The writing seems to work so and so, but when I print the values I've read, the "float amount" -value in Ingredient is something like: 686098745690300000.00000 and after that everything else is garbage too. When I write the file as normal text-file everything seems to be there (all the numbers, int and float, are "binary" but text is there). So, where did I go wrong (please, be gentle)?
I will check the return values of each function ( if(fread(....) != 1) dosomething...) when I know I can make it work. :)