Reputation: 11
This is a simple function I am making in order to learn how to copy data from files into structs.
#include <stdio.h>
#include <stdlib.h>
#define FILENAME "studentlist.txt"
struct node {
char *name[20];
int age;
struct node *next;
};
typedef struct node Node;
int main(){
Node *p,*head = malloc(sizeof(Node));
Node **listPtr;
*listPtr = head;
p = head;
int num;
FILE *students;
if ((students = fopen(FILENAME,"r")) == NULL)
printf("Error: Cant open file\n");
else
{
num = fscanf("%i", &num);
int i = 0;
for (i = 0; i <= num; i++){
fscanf(students,"%s %i", p->name, p->age);
p = p->next;
p = malloc(sizeof(Node));
}
return 0;
}
The errors I am getting are related to the fscanf function. I am also getting
expected 'struct FILE * __restrict__' but argument is of type 'char *'
What am I doing wrong?
Upvotes: 0
Views: 1269
Reputation: 9648
You just forgot the FILE*
in the line.
num = fscanf("%i", &num );
That should be
num = fscanf( students, "%i", &num );
You should read the whole error. It tells you which line and possibly the character that is giving you an issue. They tell you the problem, which is 90% of solving it.
Upvotes: 2
Reputation: 9680
The signature of the standard library function fscanf
is
int fscanf(FILE *stream, const char *format, ...);
Therefore, the following statement is wrong -
num = fscanf("%i", &num);
The format string literal "%i"
evaluates to a pointer to its first element, which is of type char *
. However, fscanf
expects the first argument of type FILE *
. This explains the error statement. Replace this line with -
num = fscanf(students, "%i", &num);
Upvotes: 1