I am having trouble reading in a file into a typedef struct that includes an enum. I am a beginner in C, so I don't really know how to read in the file with the enum.
I am able to read the file and print out the contents in simple code, but I need to read the file in and assign each string in the line into the type from the typedef struct.
The input file ooks like this:
random.num year model category
Example:
54 2012 model1 type1
These are the relevant parts of my code:
typedef enum { type1, type2, type3, type4} category;
typedef struct {
int num;
int year;
char make[MAX_MAKE_CHARS];
category category; //enum from above
}item;
//reading in the file I have this:
int create_db(char *f){
char file_contents[100]; // read the file --> ("r") FILE *f = fopen(f, "r");
// check if file can be used
if(f == NULL){
printf("File not found");
exit(1);
}
int i = 0; item item_array[100];
while(fscanf(f, "%d %d %s %s", &item[i].num, &item[i].year, item[i].make, item[i].category) != EOF){
// can't get past the while look where it says "item[i].category
I get the error:
format ‘%s’ expects argument of type ‘char *’, but argument 6 has type ‘category * {aka enum *}’
Since I am completely new to C, I am confused on how to read in the file into a struct. What do I do for the item[i].category?