Programming languages like ‘C’, Assembly and Java are used in embedded system development. Of these, ‘C’ is most commonly used because it is more convenient to interface with the hardware.
Writing data to the system disk, or reading data from the disk, or both the operations, are sometimes required in some embedded system projects using ‘C’ file functions. Here’s an application program that describes the use of these functions. We are all familiar with phonebook application in mobiles. This program creates a phonebook application in the PC using ‘C’ language.
‘C’ file functions
A file has to be opened before performing read and write operations. Opening the file creates a link between the operating system and the file functions.
The main functions in the file system are fopen and fclose. The fopen function is used to open the file. Before opening the file, you have to declare a file pointer. It returns a pointer to the file structure that it creates. Specify the name of the file to be opened and its mode to the operating system. These important tasks are carried out by the file structure that is defined in the stdio.h header file.
The syntax for declaring a file pointer and opening a file is given below:
where the file_name.txt is replaced with your desired file name and the mode indicates the operation that you want to perform on the file.
There are mainly three different modes of operation: read, write and append. The read mode is indicated by ‘r’ character. The character placed in between “ ” is considered as a string in ‘C’ language. This mode opens a pre-existing file for reading. If the file doesn’t exist, the compiler returns null to the file pointer.
The write mode is indicated by ‘w’ character. This mode opens a new file on the disk for writing. If the file already exists, it will be overwritten without confirmation.
The append mode is indicated by ‘a’ character. In append mode, you can write as well as read data. In the case of write operation, the new data is appended at the end of the file.
Some other functions used in this program are getc, putc and fclose.
The getc function returns the next character from the stream referred to by fp. It returns EOF if end of file is reached.
The syntax for getc function is:
putc is an output function. It writes the character to the file fp and returns the character written, or EOF in case of any error. The syntax for putc function is:
fclose is a simple function that takes only one argument, i.e., fp. The syntax for fclose is:
Along with the above-mentioned functions, you need some specific commands. For example, when you want to open a file, you must specify how it is to be opened. You need to specify whether you want to open an existing file, or modify the file, or create a new file. A read, write or append operation has to be mentioned. This is done using one or more file mode specifiers, which are single letters “r”, “w”, “a”,”r+”, “w+” and “a+”.
“r” opens the file for reading. It fails if the file does not exist or cannot be found.
“w” opens the file as an empty file for writing. If the file exists, its content is discarded.
“a” opens the file for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; it creates the file first if it doesn’t exist.
Adding ‘+’ to the file mode creates three new modes: “r+”, “w+” and “a+”
“r+” opens the file for both reading and writing. (The file must exist.)
“w+” opens the file as an empty file for both reading and writing. If the file exists, its content is discarded.
“a+” opens the file for reading and appending. It creates the file first if the file doesn’t exist. The appending operation includes removal of the EOF marker before new data is written to the file. The EOF marker is restored after writing is complete.
The program is compiled using Turbo C++ version 3.0 IDE. Its operation is simple. The program output is shown in the screenshot. It gives three options: ‘New entry,’ ‘Show all entries’ and ‘Find.’
New entry. This option allows the user to save new contacts into the phonebook. For this purpose, the file is opened in mode ‘0’, where every new contact is stored at the end of the file. Here the data entered by you is stored on the disk or data storage system. All the entries become the database.