introduction to file handling in c

the data entered till now will be stored in temporary memory, which we cannot use for further use.so we need to use physical media or secondary storage devices so file handling in c introduced.

by this file management, we can move data from one place to another place.it can be used offline. we can save subparts of a program .there are two types of files 1)test files 2)binary files

  1. text files are used in daily bias. they can be created by words or notepad .the common extension is .txt .when we open these files we can see the whole content in a plain text. we can edit or delete these content .
  2. binary files are different from text files .common extension is .bin .they store data in the binary format(0 and 1s).they are not readable .it has more security than text files .

File Operations

  1. Creating a new file
  2. Opening
  3. Closing a file
  4. Reading from and writing information to a file

1. Creating a new file

before writing any information , we must open a data file. this establishes a link between program and operating system. we have provide the file name and mode for opening the file. for opening files, you need to declare a pointer of type file. This declaration is needed for communication between the file and program.

FILE *fp;

we have all file handling functions in header file stdio.h .

the syntax for opening a file

pointer = fopen("path of the file","mode")

example:

ptr = fopen("c:\\windows32\sample.txt","w")

if the file given in the above example doesn't exist. The computer will create automatically on that name. And opens it because we are using w in mode .The writing mode allows you to create and edit (overwrite) the contents of the file. lets look into other options

2. Opening (modes)

File ModeMeaning of ModeDuring Inexistence of file
ropen for readingif the file doesn't exists. Open() will return null
rbopen for reading in binary modeif the file doesn't exists .fopen() will return null
wopen for writingif the file exists.it contents will be overwritten. If the file doesn't exist .it will be created
wbopen for writing in binary modeIf the file exists, its contents are overwritten. If the file does not exist, it will be created.
aopen for append .data is added at the end of the fileif the file doesn't exists ,it will be created
abopen for append in binary mode. Data is added to the end of the fileif the file doesn't exists ,it will be created
r+open for both writing and readingif the file doesn't exists.fopen() returns null
rb+open for both reading and writingif the file doesn't exists .fopen() returns null
w+open for both reading and writingif the file exists contents will be overwritten. If the file doesn't exists ,it will be created
wb+open for reading and writing in binary modeif the file exists contents will be overwritten. If the file doesn't exists ,it will be created

3. Closing a file

for closing any file we use same file pointer. The file (both text and binary) should be closed after reading/writing. Closing a file is performed using library function fclose(). fclose(fptr); //fptr is the file pointer associated with file to be closed.

fclose(ptr);