for (struct part *p = first; p != NULL; p = p->next;) {
fwrite(&(p->num), sizeof(int), 1, inventory);
fwrite(&(p->qoh), sizeof(int), 1, inventory);
fwrite(p->name, sizeof(char), strlen(p->name) + 1, inventory);
}
p is a pointer to a struct instance that looks like this:
struct part{
int num;
int qoh;
char *name;
struct part *next;
};
If i store the number 8, 104, 3176, etc. in num, things are fine. However, if I store 128, 32768, or any number that uses the most significant bit of a byte (any number with a byte 1XXX-XXX), fwrite writes all 1's into all of the more significant bytes in the file, turning 128 to -128, 199 to -57, etc.
This changing of the bytes doesn't happen during program execution - only on saving to file. Looking at num = 199 and qoh = 3, after written to file, the bytes are as such:
ff ff ff c7 00 00 00 03
which should be
00 00 00 c7 00 00 00 03
I load the file into the inventory program and it loads as expected looking at the bytes. num = -57, not 199.
Am I using fwrite wrong?
Here's the storing part, as requested.
void part_new(void) {
struct part *new;
int num, qoh;
char *name;
printf("\nEnter a part number: ");
scanf("%d", &num);
while(getchar() != '\n');
for (struct part *p = first; p != NULL; p = p->next) {
if (p->num == num) {
printf("Duplicate part number, name: %s. Canceling action.\n", p->name);
return;
}
}
printf("Enter a name: ");
if ((name = input(PART_NAME_MAX_CHARS)) == NULL) {
printf("Bad input.\n");
return;
}
printf("Enter a QOH: ");
scanf("%d", &qoh);
while(getchar() != '\n');
new = malloc(sizeof(struct part));
new->num = num;
new->qoh = qoh;
new->name = malloc(strlen(name) + 1);
strncpy(new->name, name, strlen(name) + 1);
free(name);
part_into_list(new);
return;
}
and the save function:
bool save_file(char *fname) {
FILE *inventory;
struct part *del;
if ((inventory = fopen(fname, "wb")) == NULL) {
printf("\nCould not save %s.\n", fname);
return false;
}
fseek(inventory, 0 , SEEK_SET);
for (struct part *p = first; p != NULL; p = p->next) {
fwrite(&(p->num), sizeof(int), 1, inventory);
fwrite(&(p->qoh), sizeof(int), 1, inventory);
fwrite(p->name, sizeof(char), strlen(p->name) + 1, inventory);
}
printf("%s saved.\n", fname);
fclose(inventory);
return true;
}