Text Files and Binary Files in C Language



A file is a collection of data stored in secondary memory, used for storing information that can be processed. Files allow data to be saved and retrieved later. They can store both programs and data, making file input and output operations crucial for reading from and writing to files.

There are two types of files in C language which are as follows ?

  • Text file
  • Binary File

Text File

Text files contain alphabets and numbers that are easily understood by humans. Errors in text files can be easily identified and corrected. Each character in a text file is stored as one byte. For instance, the integer value 4567, which occupies 2 bytes in memory, will take up 5 bytes in a text file. The data format is typically line-oriented, with each line representing a separate command.

C provides different functions to work with text files. Some functions are used to read the text files and some functions are used to write text file into a disk.

  • fgets() & fputs()
  • fscanf() & fprint()
  • fread() & fwrite()
  • fgetc() & fputc()

Here are the function prototypes ?

Function Description
fscanf() Reads input from the current file of the specified stream.
fgetc() Reads a single character from the specified file, and increases the file position.
fgets() This reads the single line of text or a string from the given file.
fprintf() This prints the content in the file.
fputc() This converts c to a different character and then outputs it at the current position.
fputs() This converts a string of characters to a file at a specific location.
fwrite() This allows us to write data to a binary file.

Binary file

Binary files consist of 1's and 0's, which are easily understood by computers. Errors in binary files can also duplicate the file and these are very difficult to detect. In a binary file, the integer value 1245 determines 2 bytes both in the file and memory. For example, an MP3 file, created by a sound recorder or audio editor, can be played but not in an image viewer or database software. The steps to copy a binary file are as follows ?

  • Open the source file in binary read mode.
  • Open the destination file in binary write mode.
  • Read a character from the file. The file pointer starts at the beginning, so no need to position it.
  • If feof() indicates the end of the file, then close both files and return to the calling program.
  • If the end of the file is not reached, then write the character to the destination file and repeat the process.

Files are classified into the following

  • Sequential files ? Here, data is stored and retained sequentially.
  • Random Access Files ? Here, data is stored and retrieved randomly.

Write a Binary File

The fwrite() function is used to write a block of binary data to a file. It can also be used for text. Pointers in the memory block represent the data that is copied, based on the size of each element. This function writes data to a file up to the specified number of elements. Primarily, fwrite() writes a block of data from the memory to a file.

The fwrite() function writes data to a file and returns the number of items written. If an error occurs, this number will be less than the specified count.

Syntax

Following is the syntax for writing a binary file ?

size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);

Read a Binary File

The fread() function is C reads a specified number of data elements from a file, where each element can have a different byte size. It is used for reading binary data from a file, which can include structures, arrays, or complex data types. This function is also used for reading any type of data from the file.

Syntax

Following is the syntax ?

size_t fread(void *ptr, size_t size, size_t count, FILE *stream);

Example

In the below program, we write "Hello, World!" to a file using fputs(), then read it back using fgets() and print the data.

#include <stdio.h>

int main() {
    FILE *fp = fopen("sample.txt", "w");
    fputs("Hello, World!
", fp); fclose(fp); fp = fopen("sample.txt", "r"); char buffer[20]; fgets(buffer, 20, fp); printf("%s", buffer); fclose(fp); return 0; }
Updated on: 2024-12-12T17:26:32+05:30

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements