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
- 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 .
- 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 .
- Creating a new file
- Closing a file
- 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.
we have all file handling functions in header file stdio.h .
the syntax for opening a file
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 Mode||Meaning of Mode||During Inexistence of file|
|r||open for reading||if the file doesn't exists. Open() will return null|
|rb||open for reading in binary mode||if the file doesn't exists .fopen() will return null|
|w||open for writing||if the file exists.it contents will be overwritten. If the file doesn't exist .it will be created|
|wb||open for writing in binary mode||If the file exists, its contents are overwritten. If the file does not exist, it will be created.|
|a||open for append .data is added at the end of the file||if the file doesn't exists ,it will be created|
|ab||open for append in binary mode. Data is added to the end of the file||if the file doesn't exists ,it will be created|
|r+||open for both writing and reading||if the file doesn't exists.fopen() returns null|
|rb+||open for both reading and writing||if the file doesn't exists .fopen() returns null|
|w+||open for both reading and writing||if 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 mode||if 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.