-1

I am reading input data from a file "animals.dat" which can be found here https://drive.google.com/open?id=18olXBhRgpGyY0bhpjDSwla2XcBnWoFGM

The instructions I have for this are : If the user enters 3, you are to assume that the input file animals.dat consists of records of animal type (defined below) and you need to ask the user which animal record to find and display to the screen – indicated by an integer denoting the record number. The animal type structure is defined as:

#pragma pack(1)
struct animal {
    short int id;
    char name[20];
    char species[35];
    char size;
    short int age;
};

Since the structure memory layout is different on different machines, the pragma pack directive forces the compiler to store variables of animal type the same way (using the same alignment / padding) regardless of the platform. Note that this type of search is to use random file access and that the record number and the id of an animal are the same entity. For example, if the file consists of entries like:

1,Allegra,Pseudois nayaur,S,5

2,unknown,Ailurus fulgens,X,10

3,Athena,Moschus fuscus,X,2

then the 3rd record on that file is

3,Athena,Moschus fuscus,X,2

All animals are listed in the increasing order by their id number, starting at value 1. If there is a hole in id numbers, e.g. 2, then the structure information is still present in the file, except the name component contains the string "unknown" to signify an empty record. Make sure your search uses random file processing. If an invalid id is entered, in this example any value other than 1 or 3, your program is to display an error message. Otherwise, animal record is displayed. In either case, the program is to go back to the initial menu.

Now, first 2 options are working fine. For option 3 I have been using this code:

#pragma pack(1)
struct animal {
    short int id;
    char name[20];
    char species[35];
    char size;
    short int age;
};
typedef struct animal* Animal;

void choice3(FILE *infile) {
    Animal tempAnimal;
    int id;
    printf("Enter ID ");
    scanf(" %d", &id);
    //fseek(infile,id * sizeof(struct animal),SEEK_SET);
    while(fread(&tempAnimal,sizeof(struct animal) - 1,1,infile) == 1) {
        printf("%d -- %s\n",tempAnimal->id,tempAnimal->name);   
    }    
}

Now, for some reason I am unable to store anything to my variable tempAnimal and it's throwing a segmentation fault: 11

kbreezy
  • 19
  • 2

1 Answers1

2

Perhaps your problem is happening because tempAnimal is a pointer to an Animal type. In the while statement you have put the & precedingtempAnimal which gets its memory address. Thus, you are passing a double pointer to an Animal as the first argument to fread()

fread() takes a single pointer as its first argument according to documentation: https://www.tutorialspoint.com/c_standard_library/c_function_fread.htm

Sniper
  • 1,428
  • 1
  • 12
  • 28
Vltava
  • 39
  • 3
  • 3
    Not only that, but he has failed to allocate memory for tempAnimal, but he is reading data into it. – David Hoelzer Dec 04 '17 at 03:04
  • Ah. But, now that I switched &tempAnimal to tempAnimal and sizeof(struct animal) -1 to sizeof(struct animal). It still somehow gives me the same segmentation fault: 11. – kbreezy Dec 04 '17 at 03:42
  • As David Hoelzer said, you need to also allocate memory to tempAnimal e.g. with a call to malloc() this is probably what's causing the segmentation fault. – Vltava Dec 04 '17 at 05:17