Class 12 Computer Science Ch 1: Programming in C++

Class 12 C++ Binary File Handling & OOP Lab

Class 12 C++ Practical Board Problem: Binary File Handling
#include <fstream.h>
#include <iostream.h>

class Item {
    int itemNo;
    char name[30];
    float price;
public:
    void getDetails() {
        cout << "Enter Item No, Name & Price: ";
        cin >> itemNo >> name >> price;
    }
    void showDetails() {
        cout << itemNo << " | " << name << " | Rs. " << price << endl;
    }
};

void writeToFile() {
    Item it;
    ofstream fout("ITEMS.DAT", ios::binary | ios::app);
    it.getDetails();
    fout.write((char*)&it, sizeof(it));
    fout.close();
}
Current Chapter
Programming in C++

We use essential cookies to maintain secure sessions and prevent CSRF attacks. With your consent, we and our authorized partner (Google AdSense) also use cookies to serve relevant educational advertisements and evaluate traffic. Learn more in our Cookie Policy and Privacy Policy.