+ 2

Reading a string from a file and writing it into a structure [C]

I have a text file that looks like this: Tony 98 Alex 76 Emily Rose 88 Structure that I need to load data into looks like this: struct Student{ char name[20]; int score; }array[100]; I am using fscanf for loading data into array of structures like this: while(i<100 && fscanf(input,"%20s %d",array[i].name, &array[i].score)==2) i++; I encounter a problem with "Emily Rose" since it only loads "Emily" and stops there. I know how to solve this problem if name and score are separated by something that's not a space(for example a comma): while(fscanf(filename,"%d",&niz[i].score)==1){ i=-1; do{ if(i<20) i++; name[i]=fgetc(filename); }while(name[i]!=',' && name[i]!=EOF); name[i]='\0'; } This would work if my file looked like this: 88,Emily Rose How can I make it work if there are spaces between names, and name and score?

12th Jan 2020, 4:27 PM
ja008
ja008 - avatar
14 Respuestas
+ 2
Use while(i<100 && fscanf(input,"%[^0-9] %d ",array[i].name, &array[i].score)> 0) %[^0-9] reads everything BUT digits... %d then reads digits part... the space after %d is needed to get rid of \n...
12th Jan 2020, 6:20 PM
unChabon
unChabon - avatar
+ 2
Ipang with s it doen't work as required by ja008 try it! :) https://code.sololearn.com/c04euePfXVF1/?ref=app
12th Jan 2020, 6:41 PM
unChabon
unChabon - avatar
+ 2
Ipang If you put s after [^0-9] it will be treated as a literal.. If your input doesn't have this literal scanf ends... Look at this example: https://code.sololearn.com/clHU3Q9eD28h/?ref=app
12th Jan 2020, 7:26 PM
unChabon
unChabon - avatar
+ 1
ja008 %[^0-9] matches all but digits .. So \n matches...
12th Jan 2020, 7:31 PM
unChabon
unChabon - avatar
+ 1
Exactly... That's why [^\n] doesn't work in this case.... Read ja008 's question... :)
14th Jan 2020, 2:47 PM
unChabon
unChabon - avatar
0
diego Code 👍 Considering the "name" field is a string, would it matter if the format specifier didn't have 's' in it? maybe something like "%[^0-9]s %d " instead?
12th Jan 2020, 6:38 PM
Ipang
0
diego Code I see, can you help me understand why it doesn't work? is it because of the space in the name value in the file, or something else? I'm just wanting to learn 👌
12th Jan 2020, 6:45 PM
Ipang
0
diego Code it works! However, it works whether I put space before %d or not. But If I swap places of name and score in my file and in fscanf, it prints data with \n in between. Why is that?
12th Jan 2020, 6:54 PM
ja008
ja008 - avatar
0
Use %[^\n] instead of %s,it will work
14th Jan 2020, 2:08 PM
Jaswanth Kishore S
0
Jaswanth Kishore S ja008 want's to read a string (may include spaces ) AND an integer. With [^\n] will read the whole line...
14th Jan 2020, 2:12 PM
unChabon
unChabon - avatar
0
https://code.sololearn.com/cv75URLE8n0v/?ref=app Used %[^\n] read the name n %d read the score
14th Jan 2020, 2:35 PM
Jaswanth Kishore S
0
Jaswanth Kishore S if you enter One two 56 in your program You get: "One two 56 and 0"
14th Jan 2020, 2:38 PM
unChabon
unChabon - avatar
0
Got ask r u making sure to split 2 inputs?
14th Jan 2020, 2:38 PM
Jaswanth Kishore S
0
U gotta enter "name name" then press enter which splits ur second input to a second line,then enter "score"
14th Jan 2020, 2:39 PM
Jaswanth Kishore S