C - fscanf



File Operations in Tamil


 c-fscanf

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 fscanf

C Syntax

fscanf (FILE *stream, const char *format,…..);
 c-fscanf

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 :

  1. In this statement we create the character array and the variable name as “str”.
  2. 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.
  3. In this statement we write the content in the text file using fprintf function.
  4. In this statement we use the rewind function used to find the position of the string.
  5. In this statement we read the input string using fscanf function.
  6. After the file manipulation the file will be closed using fclose function.
  7. Here in this printf statement we print the text file as string.

Sample Output - Programming Examples

  1. Here in this output the written statement “WelcomeToWikitechy” in the sample.txt file using fprintf function as shown here.
  1. 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

 c-fscanf

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


View More Quick Examples


Related Searches to C fscanf