What are the 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() & fprintf()
  • 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.

Example: Text File Operations

This example demonstrates creating a text file and reading its contents −

#include <stdio.h>

int main() {
    FILE *fp;
    char buffer[50];
    
    /* Write to text file */
    fp = fopen("sample.txt", "w");
    if (fp != NULL) {
        fputs("Hello, World!<br>", fp);
        fputs("This is a text file.<br>", fp);
        fclose(fp);
        printf("Text file created successfully.<br>");
    }
    
    /* Read from text file */
    fp = fopen("sample.txt", "r");
    if (fp != NULL) {
        printf("Reading from text file:<br>");
        while (fgets(buffer, sizeof(buffer), fp) != NULL) {
            printf("%s", buffer);
        }
        fclose(fp);
    }
    
    return 0;
}
Text file created successfully.
Reading from text file:
Hello, World!
This is a text 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 occupies 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.

Syntax

Binary file operations use fread() and fwrite() functions −

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

Example: Binary File Operations

This example demonstrates writing and reading binary data −

#include <stdio.h>

int main() {
    FILE *fp;
    int numbers[] = {10, 20, 30, 40, 50};
    int read_numbers[5];
    int i;
    
    /* Write binary data */
    fp = fopen("data.bin", "wb");
    if (fp != NULL) {
        fwrite(numbers, sizeof(int), 5, fp);
        fclose(fp);
        printf("Binary data written successfully.<br>");
    }
    
    /* Read binary data */
    fp = fopen("data.bin", "rb");
    if (fp != NULL) {
        fread(read_numbers, sizeof(int), 5, fp);
        fclose(fp);
        
        printf("Reading binary data:<br>");
        for (i = 0; i < 5; i++) {
            printf("Number %d: %d<br>", i + 1, read_numbers[i]);
        }
    }
    
    return 0;
}
Binary data written successfully.
Reading binary data:
Number 1: 10
Number 2: 20
Number 3: 30
Number 4: 40
Number 5: 50

File Types Classification

Files are also classified based on access method −

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

Conclusion

Text files store human-readable data with each character as one byte, while binary files store data in machine format for efficient storage. Use appropriate file modes ("r", "w" for text; "rb", "wb" for binary) based on your data type.

Updated on: 2026-03-15T13:10:50+05:30

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements