Many of us know the traditional uses of scanf. Well, here are some of the lesser known facts
How to read only a part of the input that we need? For example, consider some input stream that contains only characters followed by an integer or a float. And we need to scan only that integer or float.
That is ,
Input: “this is the value 100”,
Output: value read is 100
Input : “this is the value 21.2”,
Output : value read is 21.2
Now, assume we don’t know what the preceding characters are but we surely know that the last value is an integer. How can we scan the last value as an integer?
Below solution works only if input string has no spaces.
Explanation: The %*s in scanf is used to ignore some input as required. In this case, it ignores the input until the next space or new line. Similarly if you write %*d it will ignore integers until the next space or new line.
The above fact may not seem as an useful trick at the first glance. Inorder to understand its usage, let us first see fscanf().
fscanf() : Tired of all the clumpsy syntax to read from files? well, fscanf comes to the rescue.
int fscanf(FILE *ptr, const char *format, ...)
fscanf reads from a file pointed by the FILE pointer (ptr), instead of reading from the input stream.
Consider the following text file abc.txt
NAME AGE CITY abc 12 hyderbad bef 25 delhi cce 65 bangalore
Now, we want to read only the city field of the above text file, ignoring all the other fields. A combination of fscanf and the trick mentioned above does this with ease
CITY hyderbad delhi bangalore
Exercise: Count the number of words, characters and lines in a file using fscanf!
This article is contributed by Nikhil Chakravartula. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Inbuilt library functions for user Input | scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s
- Problem with scanf() when there is fgets()/gets()/scanf() after it
- Difference between scanf() and gets() in C
- Return values of printf() and scanf() in C/C++
- Cin-Cout vs Scanf-Printf
- Why "&" is not used for strings in scanf() function?
- Use of & in scanf() but not in printf()
- Why to use fgets() over scanf() in C?
- scanf("%[^\n]s", str) Vs gets(str) in C with Examples
- OpenGL program for Simple Ball Game
- OpenGL program for simple Animation (Revolution) in C
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- Lex program to take input from file and remove multiple spaces, lines and tabs
- Similarities and Differences between Ruby and C language
- Code Optimization Technique (logical AND and logical OR)
- Getting System and Process Information Using C Programming and Shell in Linux
- INT_MAX and INT_MIN in C/C++ and Applications
- Difference and Similarities between PHP and C
- What are the differences between bitwise and logical AND operators in C/C++?
- Change/add only one character and print '*' exactly 20 times