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





Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.




Comments and Discussions





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.




close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.