fscanf(), fprintf(), ftell(), rewind() functions are file handling functions in C programming language. Please find below the description and syntax for each above file handling functions.
Declaration & Description
|fscanf()||Declaration: int fscanf(FILE *fp, const char *format, …)
fscanf() function is used to read formatted data from a file. In a C program, we use fscanf() as below.
fscanf (fp, “%d”, &age);
|fprintf()||Declaration: int fprintf(FILE *fp, const char *format, …)
fprintf() function is used to write formatted data into a file. In a C program, we use fprinf() as below.
Where, fp is file pointer to the data type “FILE”.
|ftell()||Declaration: long int ftell(FILE *fp)
ftell function is used to get current position of the file pointer. In a C program, we use ftell() as below.
|rewind()||Declaration: void rewind(FILE *fp)
rewind function is used to move file pointer position to the beginning of the file. In a C program, we use rewind() as below.
Example program for fscanf(), fprintf(), ftell(), rewind() functions in C programming language:
This file handling C program illustrates how to read and write formatted data from/to a file.
int main ()
char name ;
int age, length;
fp = fopen ("test.txt","w");
fprintf (fp, "%s %d", “Fresh2refresh”, 5);
length = ftell(fp); // Cursor position is now at the end of file
/* You can use fseek(fp, 0, SEEK_END); also to move the
cursor to the end of the file */
rewind (fp); // It will move cursor position to the beginning of the file
fscanf (fp, "%d", &age);
fscanf (fp, "%s", name);
printf ("Name: %s \n Age: %d \n",name,age);
printf ("Total number of characters in file is %d", length);
Total number of characters in file is 15
Other Inbuilt file handling functions in C programming language:
C programming language offers many other inbuilt functions for handling files. They are given below. Please click on each function name below to know more details, example programs, output for the respective file handling function.
File handling functions
|fopen ()||fopen () function creates a new file or opens an existing file.|
|fclose ()||fclose () function closes an opened file.|
|getw ()||getw () function reads an integer from file.|
|putw ()||putw () functions writes an integer to file.|
|fgetc ()||fgetc () function reads a character from file.|
|fputc ()||fputc () functions write a character to file.|
|gets ()||gets () function reads line from keyboard.|
|puts ()||puts () function writes line to o/p screen.|
|fgets ()||fgets () function reads string from a file, one line at a time.|
|fputs ()||fputs () function writes string to a file.|
|feof ()||feof () function finds end of file.|
|fgetchar ()||fgetchar () function reads a character from keyboard.|
|fprintf ()||fprintf () function writes formatted data to a file.|
|fscanf ()||fscanf () function reads formatted data from a file.|
|fputchar ()||fputchar () function writes a character onto the output screen from keyboard input.|
|fseek ()||fseek () function moves file pointer position to given location.|
|SEEK_SET||SEEK_SET moves file pointer position to the beginning of the file.|
|SEEK_CUR||SEEK_CUR moves file pointer position to given location.|
|SEEK_END||SEEK_END moves file pointer position to the end of file.|
|ftell ()||ftell () function gives current position of file pointer.|
|rewind ()||rewind () function moves file pointer position to the beginning of the file.|
|getc ()||getc () function reads character from file.|
|getch ()||getch () function reads character from keyboard.|
|getche ()||getche () function reads character from keyboard and echoes to o/p screen.|
|getchar ()||getchar () function reads character from keyboard.|
|putc ()||putc () function writes a character to file.|
|putchar ()||putchar () function writes a character to screen.|
|printf ()||printf () function writes formatted data to screen.|
|sprinf ()||sprinf () function writes formatted output to string.|
|scanf ()||scanf () function reads formatted data from keyboard.|
|sscanf ()||sscanf () function Reads formatted input from a string.|
|remove ()||remove () function deletes a file.|
|fflush ()||fflush () function flushes a file.|
1. Online Compilers - Compile & Run your C/C++ programs:
2. Ask your questions or clarify your/others doubts from,