Why files are needed ??
When a program is Finished , the data is lost. Storing data in a file will preserve your data even if the program terminates.
data can easily move from one computer to another without any changes .
File I/O:-
it is necessary to store the data in a manner that can be later retrieved and displayed either in a part or in whole. This medium is usually a “file” on the disk. File I/O can be handled by using different functions.
a) Formatted functions:- The file input function fscanf( ) and the file output function fprintf( ) are called formatted file I/O functions.
b)Unformatted functions:- The input functions like getc( ), getw( ), and fread( ) are called unformatted file input functions and putc( ), putw( ), and fwrite( ) functions are unformatted file output functions. Each and every function is having its own syntax and meaning.
File streams:- Stream is either reading or writing of data. The streams are designed to allow the user to access the files efficiently. A stream is a file or physical device like key board, printer, monitor, etc., The FILE object uses these devices. When a C program is started, the operating system is responsible for opening three streams: standard input stream (stdin), standard output stream (stdout), standard error(stderr).Normally the stdin is connected to the keyboard, the stdout and stderr are connected to the monitor.
Files
File is a collection of bytes that is stored in Hard disk.
All files related function are available in stdio.h header file.
Types of Files
When dealing with files, there are two types of files you should know about:
1. Text files
2. Binary files
1. Text files
Text files are the normal .txt files that you can easily create using Notepad or any simple text editors.
When you open those files, you'll see all the contents within the file as plain text. You can easily edit or delete the contents.
They take minimum effort to maintain, are easily readable, and provide least security and takes bigger storage space.
2. Binary files
Binary files are mostly the .bin files in your computer.
Instead of storing data in plain text, they store it in the binary form (0's and 1's).
They can hold higher amount of data, are not readable easily and provides a better security than text files.
File Operations
In C, you can perform four major operations on the file, either text or binary:
Naming a file/Creation of new file
Opening an existing file
Reading data from file
Writing data into file
Closing a file
Steps for processing a file
Declare a file pointer
open a file using fopen() function
Process the file using suitable file functions.
close the file using fclose() function.
Declaration of a file
When working with files, you need to declare a pointer of type file. This declaration is needed for communication between the file and program.
Syntax
FILE *fp;
Opening a file - for creation and edit
The fopen() function is used to create a new file or to open an existing file.
General Syntax :
fp = fopen("fileopen","mode")
For Example: fopen("E:\\cprogram\\newprogram.txt","w");
fopen("E:\\cprogram\\oldprogram.bin","rb");
Closing a File
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.
File Opening Modes
Mode Description
r opens a text file in read mode
w opens a text file in write mode
a opens a text file in append mode
r+ opens a text file in read and write mode
w+ opens a text file in read and write mode
a+ opens a text file in read and write mode
rb opens a binary file in read mode
wb opens a binary file in write mode
ab opens a binary file in append mode
rb+ opens a binary file in read and write mode
wb+ opens a binary file in read and write mode
ab+ opens a binary file in read and write mode
Difference between Append and Write Mode
Write (w) mode and Append (a) mode, while opening a file are almost the same. Both are used to write in a file. In both the modes, new file is created if it doesn't exists already.
The only difference they have is, when you open a file in the write mode, the file is reset, resulting in deletion of any data already present in the file. While in append mode this will not happen. Append mode is used to append or add data to the existing data of file(if any). Hence, when you open a file in Append(a) mode, the cursor is positioned at the end of the present data in the file.
Formatted File I/O Functions
Syntax of fprintf is
fprintf (fp, “control string”, list);
Example: fprintf(fp1, “%s %d”, name, age);
Syntax of fscanf is, fscanf(fp, “control string”, list);
Example: fscanf(fp, “%s %d”, name, & age);