How To Read Input with fscanf() function In C Programming Language? – POFTUT

How To Read Input with fscanf() function In C Programming Language?

C programming language libraries provides some standard functions which can be used in different platforms like Linux and Windows. In this tutorial we will learn how to use fscan()function, return values and parameters with examples.

Declaration and Parameters

fscanf() function will accepts a file stream in FILE type and format specifiers as char type. In this case format specifiers are important because the given file will be read in this format like "%s %s %s" which means 3 strings with separated with spaces.

Return Value

fscanf functions will return data with pointers provided as parameter. But as a function it will also return the function operational status as int . If operations is successfully completed it will return 1 as integer.

Read Example

We will start with a simple example where we will read data from file named test.txt with fscanf() function in the %s %s %s format.

Our data file will be named test.txt

We will name fscanf_example.c

We will name the source code as fscanf_example.c and compile with the following gcc command.

And binary file fscanf_example can be run like below.

Read To The EOF (End Of File)

As examined in previous example we can read to the end of file with fscanf() function . We will use return value of fscanf() . If the return value is equal 1 which means a data can be read and the file is not reached end.

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)