You are on page 1of 2

Declaring and opening a file: -

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.

Different modes of the file: -


1. read ("r "): - searches for the file. If it exists, it is loaded in memory
and the pointer is set to the first character in the file. If the file does not
exists it returns NULL. Reading is possible using this mode.
2. write ("w ") : - searches for the file. If it exists, its contents are
overwritten. If the file does not exist, a new file is created. If unable to open
the file returns NULL. Writing to the file is possible.
3. append ("a "): - searches for the file. If it exists, it is loaded in memory
and the pointer is set to the last character in the file. If the file does not
exist, a new file is created. If unable to open the file returns NULL. Appending
new contents at the end of the file is possible.
4. "r+" : - searches for the file. If it exists, it is loaded in memory and the
pointer is set to the first character in the file. If the file does not exist
returns NULL. It is possible to read, write new contents, and modify existing
contents.
5. "w+" : - searches for the file. If found its contents are destroyed. If the
file is not found a new file is created. If unable to open file returns NULL.
Writing new contents, reading them back and modifying existing contents is
possible.
6. "a+" : - searches for the file. If it exists, it is loaded in memory and the
pointer is set to the. Last character in the file. If the file does not exist, a
new file is created. Returns NULL if unable to open the file. Reading existing
contents, appending new contents is possible. Cannot modify existing contents.
Multiple files can be opened and used at a given time.

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.

Unformatted File Input/ Output functions


1. fgetc and fputc functions: - The fgetc() and fputc() functions are used to
handle single character at a time. fgetc() is used to read a character from a file
and fputc() is used to write a character to a file.
Syntax: -fgetc(fp);
fputc(ch,fp);
Reading of the file should be stopped when the EOF (end of file) is
encountered.

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.*

fputs(str, fp); to write string to a file.

You might also like