How is character I/O different from Binary I/O? Explain with examples.

This answer is restricted. Please login to view the answer of this question.

Login Now

The difference between character I/O and binary I/O are

Character I/O Binary I/O
In text file, text, character, numbers are stored one character per byte i.e. 32667 occupies 5 bytes even though it occupies 2 bytes in memory. In binary file data is stored in binary format and each data would occupy the same number of bytes on disks as it occupies in memory.
In the text file, the newline character is converted to carriage-return/linefeed before being written to the disk. In binary file, conversion of newline to carriage-return and linefeed does not take place.
Text files are used to store data more user friendly. Binary files are used to store data more compactly.
In the text file, a special character whose ASCII value is 26 inserted after the last character to mark the end of file. In the binary file no such character is present. Files keep track of the end of the file from the number of characters present.
Content written in text files is human readable. Content written in binary files is not human readable and looks like encrypted content.

An example to open, read and write to a text file

#include <fstream>
#include <iostream>
using namespace std;
  
int main () {
    char data[100];
    
    ofstream myfile;
    myfile.open("hello.txt");
  
    cout << "Writing to the file" << endl;
    cout << "Enter your name: ";
    cin.getline(data, 100);
  
    myfile << data << endl;
  
    cout << "Enter your age: "; cin >> data;
    cin.ignore();
  
    myfile << data << endl;
    
    myfile.close();
    
    ifstream infile;
    infile.open("hello.txt");
  
    cout << "Reading from a file" << endl; 
    infile >> data;
  
    cout << data << endl; infile >> data;
    cout << data << endl;
  
    infile.close();
    
    return 0;
}

An example to read and write to a binary file

#include <iostream>
#include <fstream>

using namespace std;
struct Student{
    int roll_no;
    string name;
};

int main(){

    ofstream wf("student.dat", ios::out | ios::binary);

    if (!wf){
        cout << "Cannot open file!" << endl;
        return 1;
    }

    Student wstu[3];
    wstu[0].roll_no = 1;
    wstu[0].name = "Suresh";
    wstu[1].roll_no = 2;
    wstu[1].name = "Rajesh";
    wstu[2].roll_no = 3;
    wstu[2].name = "Prince";

    for (int i = 0; i < 3; i++)
        wf.write((char *)&wstu[i], sizeof(Student));

    wf.close();

    if (!wf.good()){
        cout << "Error occurred while writing!" << endl;
        return 1;
    }

    ifstream rf("student.dat", ios::out | ios::binary);

    if (!rf){
        cout << "Cannot open binary file!" << endl;
        return 1;
    }

    Student rstu[3];
    for (int i = 0; i < 3; i++)
        rf.read((char *)&rstu[i], sizeof(Student));
    rf.close();

    if (!rf.good()){
        cout << "Error occurred while reading!" << endl;
        return 1;
    }

    cout << "Student's Details:" << endl;
    for (int i = 0; i < 3; i++){
        cout << "Roll No: " << wstu[i].roll_no << endl;
        cout << "Name: " << wstu[i].name << endl;
        cout << endl;
    }

    return 0;
}

 

 

If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .