Scanning(copying) a .txt file to struct array
As the title says , i am trying to scan a .txt file which includes characters and integers and copy that to a struct array ,
file is like this :
100201 asdsad das 40 50 70
100202 fdgdf hsdhdfs 30 90 75
like this.it goes , "ID Name Surname grade1 grade2 final"
i used fscanf with %[^\n] but it just copied rest of the line.
i couldnt even manage to assign the scanned values to variables by fgetc()
any help?
i managed to do that:
Code:
main (){
int c;
struct student{
long id;
char name[15];
char surname[15];
int mid1;
int mid2;
int final;
int score;};
struct student x[10];
FILE *file;
file=fopen("grades.txt","r");
if(file==NULL){
printf("Can not open the file\n");
exit(-1);}
//i ll add for cycle after i managed to scan for (int i=0;i<10;i++)
fscanf(file,"%d %[^\n] %[^\n]s %d %d %d", &x[0].id,&x[0].name,&x[0].surname,&x[0].mid1,&x[0].mid2,&x[0].final);
system ("pause");
return 0;
}
/*i also tried this
c=fgetc(file);
while(c!=' '){
fputc(c,x[0].id);
}*/