C++ program to modify the content of a Binary File - GeeksforGeeks (2024)

// C++ program to modify the content

// of a binary file

#include <bits/stdc++.h>

using namespace std;

class abc {

int roll;

char name[20];

public:

void getdata(int, char[]);

void update(int, int, char[]);

void testcase1();

void testcase2();

void putdata();

};

// Code to display the data of the

// data of the object

void abc::putdata()

{

cout << "roll no: ";

cout << roll;

cout << "\nname: ";

cout << name;

}

// Code to set the value to the object

void abc::getdata(int a, char str[])

{

// setting the new roll no

roll = a;

// setting new name

strcpy(name, str);

}

void abc::update(int rno, int r, char str[])

{

// code to update and modify

// the content of the binary file

int pos, flag = 0;

// rno=9

fstream fs;

fs.open("he.dat",

ios::in | ios::binary | ios::out);

while (!fs.eof()) {

// storing the position of

// current file pointeri.e. at

// the end of previously read record

pos = fs.tellg();

fs.read((char*)this, sizeof(abc));

if (fs) {

// comparing the roll no with that

// of the entered roll number

if (rno == roll) {

flag = 1;

// setting the new (modified )

// data of the object or new record

getdata(r, str);

// placing the put(writing) pointer

// at the starting of the record

fs.seekp(pos);

// writing the object to the file

fs.write((char*)this, sizeof(abc));

// display the data

putdata();

break;

}

}

}

fs.close();

if (flag == 1)

cout << "\nrecord successfully modified \n";

else

cout << "\nrecord not found \n";

}

// Sample input 1

void abc::testcase1()

{

int rno, r;

char name[20];

// roll no to be searched

rno = 1;

// new roll no

r = 11;

// new name

strcpy(name, "Geek");

// call update function with new values

update(rno, r, name);

}

// Sample input 2

void abc::testcase2()

{

int rno, r;

char name[20];

// roll no to be searched

rno = 4;

// new roll no

r = 14;

// new name

strcpy(name, "Geek2");

// call update function with the new values

update(rno, r, name);

}

// Driver code

int main()

{

abc s;

// sample case 1

s.testcase1();

// sample case 2

s.testcase2();

return 0;

}

C++ program to modify the content of a Binary File - GeeksforGeeks (2024)
Top Articles
MediShare review - Is it better than insurance?
The 5 Best Stocks To Buy And Watch Right Now
How To Start a Consignment Shop in 12 Steps (2024) - Shopify
Menards Thermal Fuse
Skyward Sinton
How To Fix Epson Printer Error Code 0x9e
Canya 7 Drawer Dresser
Craftsman M230 Lawn Mower Oil Change
Www.politicser.com Pepperboy News
Athletic Squad With Poles Crossword
Snarky Tea Net Worth 2022
Top Golf 3000 Clubs
A Fashion Lover's Guide To Copenhagen
Aces Fmc Charting
Shuiby aslam - ForeverMissed.com Online Memorials
Boston Gang Map
Google Doodle Baseball 76
What Is Vioc On Credit Card Statement
Kashchey Vodka
THE FINALS Best Settings and Options Guide
F45 Training O'fallon Il Photos
Jayme's Upscale Resale Abilene Photos
Marokko houdt honderden mensen tegen die illegaal grens met Spaanse stad Ceuta wilden oversteken
Geico Car Insurance Review 2024
R Baldurs Gate 3
Meowiarty Puzzle
How To Improve Your Pilates C-Curve
Log in or sign up to view
Salemhex ticket show3
Kamzz Llc
Greater Orangeburg
Indiana Jones 5 Showtimes Near Jamaica Multiplex Cinemas
new haven free stuff - craigslist
Envy Nails Snoqualmie
Watchdocumentaries Gun Mayhem 2
Indiana Immediate Care.webpay.md
Royals op zondag - "Een advertentie voor Center Parcs" of wat moeten we denken van de laatste video van prinses Kate?
How To Get Soul Reaper Knife In Critical Legends
Craigslist Gigs Wichita Ks
Setx Sports
Amc.santa Anita
Powerboat P1 Unveils 2024 P1 Offshore And Class 1 Race Calendar
Brake Pads - The Best Front and Rear Brake Pads for Cars, Trucks & SUVs | AutoZone
Po Box 101584 Nashville Tn
Backpage New York | massage in New York, New York
Crigslist Tucson
Aurora Southeast Recreation Center And Fieldhouse Reviews
Bonecrusher Upgrade Rs3
Mytmoclaim Tracking
2487872771
91 East Freeway Accident Today 2022
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated:

Views: 6352

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.