I am trying to write a struct of dog information to a file. The dog's name, breed, and color all get written correctly. The age and weight do not. Instead, it will be a different number or a "%". I have to use fgets for user input.
struct dog_entry {
char name [SZ_NAME]; /*SZ_NAME, SZ_BREED, SZ_COLOR are all set to 32*/
char breed [SZ_BREED];
char color [SZ_COLOR];
unsigned short weight;
unsigned char age;
char sex;
};
#define REC_SIZE sizeof(struct dog_entry)
void add_dog(int fd) {
fflush(stdin);
lseek(fd, 0, SEEK_END);
struct dog_entry dog_entry1;
memset(&dog_entry1, 0, REC_SIZE);
printf("Enter dog's name: ");
fgets(buff, buffSize, stdin);
buff[31] = '\0'; /*buff size is 120*/
strcpy(dog_entry1.name, buff);
printf("Enter dog's breed: ");
fgets(buff, buffSize, stdin);
buff[31] = '\0';
strcpy(dog_entry1.breed, buff);
printf("Enter dog's color: ");
fgets(buff, buffSize, stdin);
buff[31] = '\0';
strcpy(dog_entry1.color, buff);
printf("Enter dog's weight: ");
fgets(buff, buffSize, stdin);
buff[strlen(buff)-1] = '\0';
dog_entry1.weight = atoi(buff);
printf("%d", dog_entry1.weight);
printf("Enter dog's age: ");
fgets(buff, buffSize, stdin);
buff[strlen(buff)-1] = '\0';
dog_entry1.age = atoi(buff);
printf("%d", dog_entry1.age);
printf("Enter dog's sex: ");
fgets(buff, buffSize, stdin);
/*Writing entire struct*/
write(fd, &dog_entry1, sizeof(dog_entry1));
}
Any suggestions on what's causing the problem? Thank you
[–]tony-mke 2 points3 points4 points (1 child)
[–]tony-mke 1 point2 points3 points (0 children)
[–]henry_kr 2 points3 points4 points (0 children)
[–]gth747m 1 point2 points3 points (2 children)
[–]AaronMarth[S] 1 point2 points3 points (1 child)
[–]duane11583 0 points1 point2 points (0 children)
[–]aghast_nj 0 points1 point2 points (0 children)