Files in c | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Files in c

What ‘s the different between fgets and fscanf?

27th May 2019, 11:13 PM
maedeh.f
maedeh.f - avatar
2 Answers
+ 1
fgets reads a complete line of text into a string (a char array) fscanf reads and searchs in formatted text different variables, like some numbers and characters that must be in a specific order
27th May 2019, 11:22 PM
Andres0b0100
Andres0b0100 - avatar
+ 1
fgets reads at most size characters from a file until it finds a newline and then it adds that newline to the string. fscanf reads until it finds a space and then it stops and doesn't add a newline. If you read in a string with fscanf you can overrun the buffer which you cannot do with fgets (if you gave it the right size). fscanf(fp, "%20s", str) to read in at most 20 char into string works too.
27th May 2019, 11:27 PM
Cluck'n'Coder
Cluck'n'Coder - avatar