C - fscanf
File Operations in Tamil

Learn C - C tutorial - C fscanf - C examples - C programs
C - fscanf() - Definition and Usage
- In C – Programming, the fscanf function is used to read formatted input from a stream.

C Syntax
fscanf (FILE *stream, const char *format,…..);

Sample - C Code
#include <stdio.h>
void main ()
{
char str [80];
FILE * pFile;
pFile = fopen ("sample.txt","w+");
fprintf (pFile, " %s","WelcomeToWikitechy");
rewind (pFile);
fscanf (pFile, "%s", str);
fclose (pFile);
printf ("I have read: %s \n",str);
getch();
}
C Code - Explanation :
- In this statement we create the character array and the variable name as “str”.
- Here in this statement we create a file pointer “pFile”. Here in this statement we open the text file using “fopen” function in writing mode.
- In this statement we write the content in the text file using fprintf function.
- In this statement we use the rewind function used to find the position of the string.
- In this statement we read the input string using fscanf function.
- After the file manipulation the file will be closed using fclose function.
- Here in this printf statement we print the text file as string.

Sample Output - Programming Examples

- Here in this output the written statement “WelcomeToWikitechy” in the sample.txt file using fprintf function as shown here.

- Here in this output, the statement (“WelcomeToWikitechy”) which has been read in the sample.txt file using fscanf function as shown here.
Example
struct emp
{
char name[10];
};
void main()
{
struct emp e; FILE *p;
p = fopen("one.txt", "r");
do
{
fscanf(p,"%s ", e.name);
printf("%s ", e.name);
}
while(!feof(p));
getch();
}
Output

Learn C - C tutorial - C fscanf - C examples - C programs