Pr.Pg Next Pg

File Handling tutorials

What is a file?

  • The information or data stored under a specific name on a storage device, is called a file.

 

File Handling in C

  • All File manipulation function are defined in stdio.h header file of C programming.

  • C supports file handling i.e. the data of a file can read from a file as well as written in a file. In C file IO is achive through using fopen, fwrite, and fread,fprintf, fscanf, fgetc and fputc.

 

FILE *

  • For C File I/O you need to use a FILE pointer, which will let the program keep track of the file being accessed. You can think of it as the memory address of the file or the location of the file. 

Example:

FILE *fp

 

fopen

  • To open a file you need to use the fopen function, which returns a FILE pointer.

  • Once you've opened a file, you can use the FILE pointer to let the compiler perform input and output functions on the file.

Example:

FILE *fopen(const char *filename, const char *mode);

 

  • In the above example mode indicate the file mode, there are different types of file mode as below:

Sr No

Mode

Description

1

r

Open file for reading

2

w

Open file for writing (file need not exist)

3

a

Open file for appending (file need not exist)

4

r+

Open file for reading and writing, start at beginning

5

w+

Open file for reading and writing (overwrite file)

6

a+

Open file for reading and writing (append if file exists)

Pr.Pg border                                              Next Pg