Working With Files
Working With Files
• A data file is a computer file which stores data to be used by a computer application or
system, including input and output data.
• The data and information are stored in the storage device in the form of file.
• Every file in computer is assigned a unique name called file name with extension.
• File handling or file processing is the process to create a data file, write data to the data
file, read data from the specified data file and edit the records that are present in the file.
File handling consists of three steps or procedures.
Step 1: File has to be opened in order to perform tasks. User also have to create the file, if it
is not created.
Step 2 : After opening the file user either writes data in the file or read data from the file.
Step 3: After all the work is finished, the file has to be closed.
Data files in C are broadly classified to two types :
1) High-Level files(Standard files or stream oriented files)
2) Low-Level files(System oriented files)
1) Standard files: These are often called high level data files. Standard files are of two
types:
i) Text files: Text files comprise consecutive character. These characters can be interpreted
as individual data items or as component of strings. It is created with .txt extension.
ii) Binary files: A binary file stores information in the form of the binary number(0’s and
1’s) and hence occupies less memory space. It is created with a .bin extension. Since it is
not readable to humans, the information is more secure.
2) System Oriented data files : They are more closely related to the computer operating
system. These are often called low level data files.
Types of File
Computer files are categorized into two types:
a) Program File: The file which consists of set of instructions written by using programming
language in order to process data stored in the file is called program file. It consists of source
program file and executable files.
b) Data files: The file which contains different data such as name, address, age, phone etc.
required for data processing is called data files. Data in data files are stored in table form of
rows and column. Data files is processed and managed with the help of program files.
C support two type of files organization or file access methods. They are
a) Sequential access data file
b) Random access data file
Sequential Access Random Access
Sequential access to a file means that computer system Random access to a file means that the computer system
reads or write information to the file sequentially, can read or write information anywhere in the data file.
starting from the beginning of the file and going on step
by step
The file is accessed slowly. The file is accessed quickly.
Sequential file must search through each file. Random files need not to search every files.
There is no predefined order of accessing files. C defines set of function to manipulate the position of
the file
Insertion and updating of file is difficult Insertion and updating is easy
fread(),fscanf(), function is used fseek(), ftell() function is used.
Opening, Reading, Writing and Appending Data
Files
Writing Data to a File- The file write operation can be performed by following functions:
a) fputc: The fputc() function is used to write a single character into file. It outputs a
character to a stream. Syntax :fputc(character,file pointer)
b) putc: The putc() function is a C library function, which is used to write a character to a
file. This function is used to write a single character to the stream as well as advance the
position of the indicator. Syntax: putc(chracter, File pointer)
c) putw: It is used to write integers to the file. Syntax: putw(integer variable, File pointer)
d) fputs: It is used to write a string to the file pointed to by the file pointer. Syntax:
fputs(string, file pointer).
e) fprintf: It is used to write string to the file pointed to by file pointer. The string can
include format specifiers and a list of variables.Syntax:fprintf(filepointer,”format
specifier”,list of variables).
• EOF(End of file) : When the file pointer reaches the end of file, it encounter a
character with ASCII value 26 which represents end of file. EOF is a constant defined
in the header file stdio.h which has its value -1.
Reading Data from a File
The data stored in the file must also be read in order to display the data. The function used to
read data from the file are
a) fgetc: It returns the next character from the file pointed to by the file pointer. When the
end of the file has been reached, the EOF is sent back. Syntax: fgetc(file pointer).
b)getc : It is used to read a character from file. Syntax: getc(file pointer)
c) getw: It is used to read integer value from the file. Syntax: getw(file pointer)
d) fgets: It is used to read a file line by line. Reads data until it either encounters a newline or
exceeds the specified buffer length. Syntax:fgets(char *str, int n, FILE *stream).
str - It is the variable in which the string is going to be stored
• n - It is the maximum length of the string that should be read
• stream - It is the filehandle, from where the string is to be read.
e) fscanf: It is used to read characters from the file and assigns the input to a list of
variables. Syntax: fscanf(file pointer,”format specifiers”,variable list)
Deleting file: Remove function is used to delete the file
Syntax: remove(“file name”)
Rename file: Rename function is used to rename the old file name into the new file
name. syntax: rename(“Old file name”,”New file name”)