Professional Documents
Culture Documents
Every file, which we open, has its own FILE structure, which contains
information about the file like its size, its current location in memory etc. The
FILE structure contains a character pointer, which points to the first character
that is to be read. Syntax to open file: -FILE *fp;
fp = fopen ("filename" , "mode");
fp is declared to be a pointer to the data type FILE, fp contains the address of
the structure FILE. The second statement opens the file whose name is filename and
the mode indicates the purpose of opening the file, i.e. whether to read a file or
write into a file.
Closing a File: -
When we finish all the operations on a file, the file must be closed. This
ensures that the information associated with the file is removed from the buffer
and all the links to the file are broken. We can prevent the misuse of the file,
and we need to open the same file in different mode.
Syntax: -
fclose (filepointer);
This function will close the file associated with the FILE
pointerfi/epointer. Closing a file deactivate the file and the file is no longer
accessible.
2. getw and putw functions: - getw and putw are similar to fgetc and fputc. They
are used to read integer values. Can be used when we are dealing with integer data.
Syntax: -
putw(integer, fp); to write integer to a file,
getw(fp); to read an integer from a file.
3.String I/O files (fgets and fputs): - To read a string from a file use fgets()
function and to write string to a file use fputs() function. Syntax: -
fgets(str); to read string from a file.*