scanf() and fscanf() in C – Simple Yet Poweful

3.4

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

/* C program to demonstrate that we can
   ignore some string in scanf() */
#include <stdio.h>
int main()
{
    int a;
    scanf("This is the value %d", &a);
    printf("Input value read : a = %d", a);
    return 0;
}
// Input  : This is the value 100
// Output : Input value read : a = 100

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.

/* Sample C program to demonstrate use of *s */
#include<stdio.h>
int main()
{
    int a;
    scanf("%*s %d", &a);
    printf("Input value read : a=%d",a);
    return 0;
}

// Input: "blablabla 25"
// Output: Value read : 25

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().

fscand() : 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

/*c program demonstrating fscanf and its usage*/
#include<stdio.h>
int main()
{
    FILE* ptr = fopen("abc.txt","r");
    if (ptr==NULL)
    {
        printf("no such file.");
        return 0;
    }

    /* Assuming that abc.txt has content in below
       format
       NAME    AGE   CITY
       abc     12    hyderbad
       bef     25    delhi
       cce     65    bangalore */
    char* buf[100];
    while (fscanf(ptr,"%*s %*s %s ",buf)==1)
        printf("%s\n", buf);

    return 0;
}

Output:

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 contribute@geeksforgeeks.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.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3.4 Average Difficulty : 3.4/5.0
Based on 27 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.