C++ Binary File I/O

Read & Write to a '.bin' file.

* Binary files store data in a format that consists of sequences of binary digits (bits).

Binary File I/O

The term ".bin" is often used as a file extension for binary files. Binary files can contain various types of data, such as images, audio, video, executable programs, or any other type of information that can be represented in a binary format.

Binary files are typically used for efficiency and speed in data storage and processing. They can store data more compactly than text files and are often faster to read and write. However, due to their non-human-readable nature, they are not suitable for storing information that needs to be easily interpreted by humans.

In the context of the provided C++ program, for binary file I/O, you can use the 'read()' and 'write()' functions, which work with raw data.

The program uses binary file handling functions ('std::ios::binary', 'write', and 'read') to perform these operations.

cpp Copy Code
#include<iostream>
#include<fstream>
#include<cstring>

int main() {
    // Writing to a binary file
    const char* dataToWrite = "Hello, World!";
    std::ofstream binaryOutput("data.bin", std::ios::binary);

    if (binaryOutput.is_open()) {
        binaryOutput.write(dataToWrite, std::strlen(dataToWrite));
        binaryOutput.close();
        std::cout << "Binary file 'data.bin' created successfully.\n";
    } else {
        std::cerr << "Error creating binary file\n";
        return 1;
    }

    // Reading from a binary file
    char buffer[100]; // Assuming a maximum of 100 characters
    std::ifstream binaryInput("data.bin", std::ios::binary);

    if (binaryInput.is_open()) {
        // Read binary data into the buffer
        binaryInput.read(buffer, sizeof(buffer));

        // Output the read data
        std::cout << "Read from file: " << buffer << "\n";

        binaryInput.close();
    } else {
        std::cerr << "Error opening binary file for reading\n";
        return 1;
    }

    return 0;
}
Output:
Binary file 'data.bin' created successfully.
Read from file: Hello, World!⌂

Let's break down the program:

1. Include Headers:

#include <iostream>
#include <fstream>
#include <cstring>

These are include statements for the necessary C++ standard libraries. '<iostream>' is for input and output operations, '<fstream>' is for file handling, and '<cstring>' is for C-style string manipulation.

2. Writing to a Binary File:

const char* dataToWrite = "Hello, World!";
    std::ofstream binaryOutput("data.bin", std::ios::binary);

'dataToWrite' is a C-style string containing the text "Hello, World!".

'std::ofstream' is used to create an output file stream ('binaryOutput') in binary mode ('std::ios::binary').

The file "data.bin" is opened for writing.

if (binaryOutput.is_open()) {
        binaryOutput.write(dataToWrite, std::strlen(dataToWrite));
        binaryOutput.close();
        std::cout << "Binary file 'data.bin' created successfully.\n";
    } else {
        std::cerr << "Error creating binary file\n";
        return 1;
    }

Checks if the file is successfully opened.

Writes the content of 'dataToWrite' to the binary file using the 'write' function.

Closes the file and prints a success message. If there is an error, it prints an error message and returns 1, indicating an error.

3. Reading from a Binary Files:

char buffer[100]; // Assuming a maximum of 100 characters
    std::ifstream binaryInput("data.bin", std::ios::binary);

Declares a character array 'buffer' to store the read data (assuming a maximum of 100 characters).

'std::ifstream' is used to create an input file stream ('binaryInput') in binary mode.

if (binaryInput.is_open()) {
        binaryInput.read(buffer, sizeof(buffer));
        std::cout << "Read from file: " << buffer << "\n";
        binaryInput.close();
    } else {
        std::cerr << "Error opening binary file for reading\n";
        return 1;
    }

Checks if the file is successfully opened.

Reads binary data from the file into the 'buffer' using the 'read' function.

Prints the read data to the console.

Closes the file. If there is an error, it prints an error message and returns 1.

4. Return Statement:

    return 0;

Indicates successful program execution. If there were any errors, the program would have returned 1.

What is New?

We have just updated this website & still working on adding new content & updating existing features.