In this guide, we will learn how to open, read, and write information to data files using functions from the standard input/output (
<stdio.h>) library. It is used for permanent storage.
Files are often text-based and are used for storing and retrieving related information like that stored in a database or stored in your local system.
It’s important to understand the breakdown and hierarchy of files, because each component (parent) and sub component (child) are used together to create the whole. Without each component and its hierarchical relationships, building more advanced data file systems such as relational databases would be difficult.
A common data file hierarchy is typically broken down into five categories as described below –
|bit||binary digit 0 or 1|
|Field||Grouping of bytes|
|Record||Grouping of Fields|
|File||Grouping of records|
It is also known as binary digits.
Bits are the smallest value in a data file. Each bit value can only be a 0 or 1.
Because bits are the smallest unit of measurement in computer systems, they provide an easy mechanism for electrical circuits to duplicate 1’s and 0’s. When they are grouped together, bits can build the next unit of data management, known as bytes.
Bytes provide the next step in the data file food chain. Bytes are made up of eight bits and are used to store a single character, such as a number, a letter, or any other character found in a character set.
For example, a single byte might contain the letter M, the number 7, or a keyboard character such as the exclamation point (!). Together, bytes make up words or, better yet, fields.
In database, groupings of characters are most commonly referred to as fields.
Fields are often recognized as placeholders on a graphical user interface (GUI), but are really a data concept that groups characters in a range of sizes and data types to provide meaningful information.
Fields could be a person’s name, social security number, street address, phone number, and so on.
For example, the name “Rushikesh” could be a value stored in a field called First Name. When combined in a logical group, fields can be used to express a record of information.
Records are logical groupings of fields that comprise a single row of information. Each field in a record describes the record’s attributes.
For example, a student record might be comprised of name, age, ID, major, and GPA fields. Each field is unique in description but together describes a single record.
Individual fields in records are sometimes separated or delimited using spaces, tabs, or commas as shown in the next sample record that lists field values for a single student.
Rushikesh Chaudhari, 23, 555-55-5555, Computer Science, 4.0
Together, records are stored in data files.
Files are comprised of one or more records and are at the top of the data file food chain.
Each record in a file typically describes a unique collection of fields. Files can be used to store all types of information, such as student or employee data.
Files are normally associated with various database processes in which information can be managed in nonvolatile states, such as a local disk drive, USB flash device, or web server.
An example file called students.dat with comma-delimited records is shown next.
Michael Vine, 30, 222-22-2222, Political Science, 3.5
Sheila Vine, 29, 555-55-5555, Computer Science, 4.0
Spencer Vine, 19, 777-77-7777, Law, 3.8
Olivia Vine, 18, 888-88-8888, Medicine, 4.0
Functions of File handling
Before we see the file handling functions, you should know what is stream in file input/output.
C programmers use pointers to manage streams that read and write data. Understanding streams is quite easy. In fact,
streams are just file or hardware devices, such as a monitor or printer, which can be controlled by C programmers using pointers to the stream.
To point to and manage a file stream in C, simply use an internal data structure called
FILE. Pointers of type
FILE are created just like any other variable.
There are many functions in C library to open, read, write, search and close file. A list of file functions are given below:
|fopen()||opens new or existing file|
|fprintf()||write data into file|
|fscanf()||reads data from file|
|fputc()||writes a character into file|
|fgetc()||reads a character from file|
|fclose()||closes the file|
|fseek()||sets the file pointer to given position|
|putw()||writes an integer to file|
|getw()||reads an integer from file|
|ftell()||returns current position|
|rewind()||sets the file pointer to the beginning of the file|
Example of File Operation
Let’s see the simple c program to open, read and close the file.
/* Pointer to the file */
/* Character variable to read the content of file */
/* Opening a file in r mode*/
fp1= fopen ("C:\\file.txt", "r");
/* Infinite loop –I have used break to come out of the loop*/
c = fgetc(fp1);
In the above program, we are opening a file
r mode, reading the content of the file and displaying it on the console.