Basics Operation of File Handling in C

 

1.                       Creation of a new file

 

        (fopen() with attributes as “r” or “r+” or “w” or “w++” or “a” or “a++”)

 

Mode

Description

R

opens a text file for reading

W

opens a text file for writing

A

opens a text file for appending. (appending means to add text at the end)

r+

opens a text file for reading and writing

w+

opens a text file for both reading and writing. (it first cuts the length of the file to zero if it exists, or create a file if it does not exist)

a+

opens a text file in both reading and writing mode. (It creates a file if it does not exist. Reading will start from the beginning, but writing can only be done at the end)

 

 

2.                       Opening an existing file

( fopen() )

 

3.                       Reading from file

( fscanf() or fgetc() )

 

4.                       Writing to a file

( fprintf() or fputs() )

 

5.                       Moving to a specific location in a file

( fseek() , rewind() )

 

6.                       Closing a file

( fclose() )

 

 

Basics Functions of File Handling in C

 

 

No.

Function

Description

 

1

 

fopen()

 

opens new or existing file

 

 

2

 

fprintf()

 

write data into the file

 

3

 

fscanf()

 

reads data from the file

 

4

 

fputc()

 

writes a character into the file

 

5

 

fgetc()

 

reads a character from file

 

6

 

fclose()

 

closes the file

 

7

 

fseek()

 

sets the file pointer to given position

 

8

 

fputw()

 

writes an integer to file

 

9

 

fgetw()

 

reads an integer from file

 

10

 

ftell()

 

returns current position

 

11

 

rewind()

 

sets the file pointer to the beginning of the file