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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL








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 some rights reserved.