I’m trying to assign values into the my structs. However, I’m finding some difficulty. I would like to create a list that holds users, titles, and views.
I have a struct as shown below
struct table{
char *user[50];
char *title[50];
int views;
}
I get the information from a text file and I’m trying to read the text file line by line and assigning the values accordingly.
struct table *tables;
tables = malloc(50*sizeof(struct table));
FILE *ptr_file;
char *name_file="2012-11-05-13-34.txt"; //change this later
ptr_file=fopen(name_file, "r");
if(!ptr_file)
printf("Couldn't open file %s for reading.\n", name_file);
printf("Opened file %s for reading.\n", name_file);
line_number = 0;
while(fgets(buffer, sizeof(buffer), ptr_file) != NULL){
if(strcmp(buffer, "") == 0)
return 0;
char *views=strtok(buffer, ",");
char *name=strtok(NULL, ",");
char *title=strtok(NULL, ",");
tables[line_number].views=atoi(views);
strcpy(tables[line_number].user, user);
strcpy(tables[line_number].title, title);
line_number++;
}
I’m getting errors like char*_restricted_but argument is type char**. I was wondering if anyone can help explain this to me or if they can direct me to anywhere I can get some examples I can look through.
Thanks.
struct tabledoesn’t nave anamemember itsuser.Also you have
userandtitledeclared as an array of pointers, but try to use them to hold strings, use char arrays instead.–EDIT–
If you want to keep the array of pointers (for sorting or whatever), you are going to have to allocate memory for each one to store your strings.