?>
profile image
Announcements
No new announcements.
mentora icon
bookmark
Table of Contents

    Binary File Handling in C++

    In this lesson, we will learn the basics of binary file handling in C++ programming, including creating, opening, reading, writing, and closing binary files and storing data in its original format.

    What is Binary File Handling

    Binary File Handling is a process that allows us to create a file and store data in its original format. Unlike text files, where data is treated as plain text, binary files preserve the actual representation of data types. For instance, if we store an integer value in a binary file, it is treated as an integer, not just a sequence of characters.

    Binary File Opening Modes

    Below, we discuss the different file opening modes used to open a binary file with an fstream class object.

    Mode Description
    ios::binary | ios::in This mode opens a file in binary mode for reading. The file pointer is positioned at the beginning of the file. It is the default mode when a file is opened without specifying a mode.
    ios::binary | ios::out This mode opens a file in binary mode for writing. If the file does not exist, it will be created. If the file already exists, it will be overwritten. The file pointer is positioned at the beginning of the file.
    ios::binary | ios::app This mode opens a file in binary mode for appending. If the file does not exist, it will be created. If the file already exists, new data will be added to the end of the file, preserving the existing content.
    ios::binary | ios::in | ios::out This mode is used to open a file in binary mode for both reading and writing. When the file is opened, the file pointer is initially placed at the beginning of the file.
    ios::binary | ios::in | ios::out | ios::trunc This mode is used to open a file in binary mode for both reading and writing. If the file already exists, it will be truncated to zero length (i.e., all its contents will be deleted). The file pointer is placed at the beginning of the file.
    ios::binary | ios::in | ios::out | ios::app This mode is used to open a file in binary mode for both reading and writing, and all new data will be added to the end of the file. The file pointer is positioned at the end of the file for writing operations. While reading can occur from any position in the file, all write operations will occur at the end, and it is not possible to move the write pointer to a different position within the file for writing.
    ios::binary | ios::in | ios::out | ios::ate This mode is used to open a file in binary mode for both reading and writing. When the file is opened, the file pointer is initially placed at the end of the file. Any new data written to the file will be appended to the end unless the file pointer is repositioned. You can reposition the pointer to read or write at any position within the file.

    Unlock This Lesson & Start Learning!

    Get full access to this lesson, practical exercises, quizzes, and your Mentora AI Tutor.

    Special Offer, use coupon code DWL6BC to get additional discount of 10% on all subscription. Hurry limited time offer.

    20% off

    1 Month

    ₹323
    ₹259
    Unlock Now

    Renews at ₹259/month.

    Manual renewal required.
    No automatic charges.


    • ✓ Premium lessons access
    • ✓ Quizzes and exercises
    • ✓ Mentora AI Tutor
    32% off

    3 Months

    ₹971
    ₹659
    Unlock Now

    Equivalent to ₹219.67/month.

    Manual renewal required.
    No automatic charges.


    • ✓ Premium lessons access
    • ✓ Quizzes and exercises
    • ✓ Mentora AI Tutor
    40% off

    6 Months

    ₹1942
    ₹1159
    Unlock Now

    Equivalent to ₹193.17/month.

    Manual renewal required.
    No automatic charges.


    • ✓ Premium lessons access
    • ✓ Quizzes and exercises
    • ✓ Mentora AI Tutor
    44% off

    12 Months

    ₹3884
    ₹2159
    Unlock Now

    Equivalent to ₹179.92/month.

    Manual renewal required.
    No automatic charges.


    • ✓ Premium lessons access
    • ✓ Quizzes and exercises
    • ✓ Mentora AI Tutor
    Mentora Question Asked: 0 / 20

    Hi, I’m Mentora!

    I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way.