In: Computer Science
Following are the most important file management functions available in C. Use any 6 of them in your program and show the output.
Function |
purpose |
fopen () |
Creating a file or opening an existing file |
fclose () |
Closing a file |
fprintf () |
Writing a block of data to a file |
fscanf () |
Reading a block data from a file |
getc () |
Reads a single character from a file |
putc () |
Writes a single character to a file |
getw () |
Reads an integer from a file |
putw () |
Writing an integer to a file |
fseek () |
Sets the position of a file pointer to a specified location |
ftell () |
Returns the current position of a file pointer |
rewind () |
Sets the file pointer at the beginning of a file |
Write the C code here and run it. |
Output: |
Can you type code please. Ty
code:-
#include <stdio.h>
int main ()
{
char name [20];
int age, length;
FILE *fp;
fp = fopen ("test.txt","w");
fprintf (fp, "%s %d", "Ranaveer", 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);
fclose (fp);
printf ("Name: %s \n Age: %d \n",name,age);
printf ("Total number of characters in file is %d", length);
return 0;
}
output:-
Name: Ranaveer
Age: 5
Total number of characters in file is 10
File:-
Ranaveer 5