C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs



Home » C programs » stdio.h header file functions

fscanf() function of stdio.h in C






In this article, we are going to learn about the fscanf() function of stdio.h header file in C programming language and use it to scan rewind variables.
Submitted by Abhishek Sharma, on April 13, 2018

This function is just like scanf() function, but instead of standard input, this read data from the file. Using this function is simple if you already knew about the scanf() function.

This, fscanf() function requires one more parameter then the scanf() function and that parameter is the File object. Rest of the argument are just as the scanf() function.

Syntax:

 fscanf(F, "%s", a);

Here, F is the file object, a is the character array and "%s" string denoting the input value must be string.

This will read a string from the file from the current position of the cursor. You can do same with integers etc.

stdio.h - fscanf() function Example in C

#include <stdio.h>
#include <stdlib.h>


int main ()
{
	//initializing the type of variables 
	//and a file pointer
	char a[10], b[10], c[10], d[10];
	int z;
	FILE * F;

	//opening the file
	F = fopen ("abc.txt", "w+");

	//putting string
	fputs("I love include help 1234567890", F);

	//rewind file pointer
	rewind(F);

	//scanning variables
	fscanf(F, "%s %s %s %s %d", a, b, c, d, &z);

	//printing the values
	printf("  String1 |%s|\n", a );
	printf("  String2 |%s|\n", b );
	printf("  String3 |%s|\n", c );
	printf("  String4 |%s|\n", d );
	printf("  Integer |%d|\n", z );

	fclose(F);

	return(0);
}

Output

stdio.h - fscanf() in c language






You may also be interested in...

C/C++ Tips and Tricks...

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.


Comments and Discussions