I am trying to read a file, assign the values to the structure members and then add the values into a dynamic memory allocation. I used a for loop to read all the values and then add them to the dynamic array. It gives me errors while I compile it.
How could I assign the values to the Structure members and at the same time add them to a dynamic array?
I would really appreciate your assistance !! thanks.
Here is my code
struct Student
{
char *name[20];
char *gender[10];
int age;
char *course[20];
};
struct elements
{
struct Student *s;
struct elements *next;
};
FILE *file;
char c;
char type[30];
int main(int args, char *arg[])
{
struct Student *school = (struct Student *)malloc(sizeof(struct Student)); // Dynamic array
file = fopen(arg[1], "r"); // reading file
if(file != NULL)
{
while((c = fgetc(file)) != EOF) putchar(c);
{
int size;
int r;
for (r=0; r<size;r++)
{
fscanf("Name: %s", school->name);
//school->name = Addstudent(); // didn't work
//printf("names are: %s\n",school->name); // here I want to make sure the value name is in the array
}
}
}
else
{
printf("Unable to open file \n");
}
free(file);
return 0;
}
char* Addstudent()
{
char* name;
fscanf(file, "Name: %s",type ,name);
return name;
}
Here are the Errors
In function ‘main’: Latest1.c:59:4: warning: passing argument 1 of ‘fscanf’ from incompatible pointer type [enabled by default] /usr/include/stdio.h:449:12: note: expected ‘struct FILE * restrict’ but argument is of type ‘char *’
Latest1.c:59:4: warning: passing argument 2 of ‘fscanf’ from incompatible pointer type [enabled by default] /usr/include/stdio.h:449:12: note: expected ‘const char * restrict’ but argument is of type ‘char **’
Latest1.c:59:4: warning: format not a string literal and no format arguments [-Wformat-security]