SlideShare a Scribd company logo
Disk File I/O in
C++
Dr. Syed Aun Irtaza
Department of Computer Science
University of Engineering and Technology
(UET) Taxila
Disk File I/O with Streams
O Working with disk files requires set of classes:
O ifstream for input,
O ofstream for output.
O fstream for both input and output
O Objects of these classes can be associated with disk
files, and we can use their member functions to read
and write to the files.
O The ifstream, ofstream, and fstream classes are
declared in the FSTREAM file.
Writing Data formatted I/O,
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main()
{
char ch = 'x';
int j = 77;
double d = 6.02;
string str1 = "Hello";
string str2 = "Testing";
ofstream outfile("data.txt", ios::out | ios::trunc); //create ofstream object
outfile << ch << j << ‘ ' << d << str1 << ‘ ' << str2;
cout << "File writtenn";
return 0;
}
Writing Data formatted I/O,
int main() {
char ch = ‘x’;
int j = 77;
double d = 6.02;
string str1 = “Hello”;
string str2 = “Testing”;
ofstream outfile(“data.txt”); //create ofstream object
outfile << ch << j << ‘ ‘ << d << str1 << ‘ ‘ << str2;
cout << “File writtenn”;
return 0;
}
If the file doesn’t exist,
it is created.
If it does exist, it is
truncated and the new
data replaces the old.
Reading Data formatted I/O,
int main() {
char ch;
int j;
double d;
string str1;
string str2;
ifstream infile(“fdata.txt”);
infile >> ch >> j >> d >> str1 >> str2;
cout << ch << endl << j << endl << d << endl
<< str1 << endl << str2 << endl;
return 0;
}
Strings with Embedded Blanks
#include <fstream>
int main() {
ofstream
outfile(“TEST.TXT”);
outfile << “Hello Dear !n”;
outfile << “We are writingn”;
outfile << “data in filesn”;
outfile << “with
embeddedn”;
outfile << “blanksn”;
return 0;
}
#include <iostream>
#include<fstream>
int main() {
const int MAX = 80;
char buffer[MAX];
ifstream infile(“TEST.TXT”);
while( !infile.eof() ) {
infile.getline(buffer, MAX);
cout << buffer << endl;
}
return 0;
}
Character I/O
#include <fstream>
#include <iostream>
#include <string>
int main() {
string str = “Time is a great teacher, but
unfortunately it
kills all its pupils. Berlioz”;
ofstream outfile(“TEST.TXT”);
for(int j=0; j<str.size(); j++)
outfile.put( str[j] );
cout << “File writtenn”;
return 0;
}
Character I/O
#include <fstream>
#include <iostream>
void main() {
char ch;
ifstream infile(“TEST.TXT”);
while( infile ){
infile.get(ch); //read character
cout << ch; //display it
}
cout << endl;
}
read until EOF or error
Binary I/O
O We can write a few numbers to disk using
formatted I/O, but if you’re storing a large
amount of numerical data it’s more
efficient to use binary I/O
O In binary I/O numbers are stored as they
are in the computer’s RAM memory,
rather than as strings of characters
Binary I/O
O In binary I/O an int is stored in 4 bytes,
whereas its text version might be “12345”,
requiring 5 bytes.
O Similarly, a float is always stored in 4
bytes, while its formatted version might be
“6.02314e13”, requiring 10 bytes.
Reading & Writing an Object to Disk
struct student{
int rno;
char name[25];
};
void getdata(student &s1){
cout<<"nEnter name";cin>>s1.name;
cout<<"nEnter Roll no";cin>>s1.rno;
}
void showdata(student &s2){
cout<<"nName";cout<<s2.name;
cout<<"nRoll no"<<s2.rno;
}
Reading & Writing an Object to Disk
int main() {
student std[3];
for(int i=0; i<=2; i++)
getdata(std[i]);
ofstream outfile("std.txt", ios::binary);
for(int a=0; a<=2; a++)
outfile.write(reinterpret_cast<char*>(&std[a]), sizeof(std[a]));
ifstream infile("std.txt", ios::binary);
for(int a=0; a<=2; a++){
infile.read( reinterpret_cast<char*>(&std[a]), sizeof(std[a]));
showdata(std[a]);
}
}
The Mode Bits
File pointer positions
O Each file object has associated with it two integer values
called the get pointer and the put pointer.
O These are also called the current get position and the current
put position.
O These values specify the byte number in the file where
writing or reading will take place.
O The seekg() and tellg() functions allow you to set and
examine the get pointer.
O The seekp() and tellp() functions perform these same
actions on the put pointer.
File pointer positions
O For example, will set the put pointer to 10 bytes
before the end of the file.
O seekp(-10, ios::end);
O For example, will set the put pointer to the end of
file.
O infile.seekg(0, ios::end);
File pointer positions

More Related Content

Similar to File Handling in C++ full ppt slide presentation.ppt (20)

PL-II Lecture19 oop in c++ pyaray bachon .ppt
PL-II Lecture19  oop in c++  pyaray bachon .pptPL-II Lecture19  oop in c++  pyaray bachon .ppt
PL-II Lecture19 oop in c++ pyaray bachon .ppt
inambscs4508
 
working with files
working with filesworking with files
working with files
SangeethaSasi1
 
C++ files and streams
C++ files and streamsC++ files and streams
C++ files and streams
krishna partiwala
 
ONLINE STUDENT MANAGEMENT SYSTEM
ONLINE STUDENT MANAGEMENT SYSTEMONLINE STUDENT MANAGEMENT SYSTEM
ONLINE STUDENT MANAGEMENT SYSTEM
Rohit malav
 
Introduction to files management systems
Introduction to files management systemsIntroduction to files management systems
Introduction to files management systems
araba8
 
Vcs28
Vcs28Vcs28
Vcs28
Malikireddy Bramhananda Reddy
 
Streaming and input output mOOPlec9.pptx
Streaming and input output mOOPlec9.pptxStreaming and input output mOOPlec9.pptx
Streaming and input output mOOPlec9.pptx
areebakanwal12
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdf
anandatalapatra
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
Dr .Ahmed Tawwab
 
Managing console i/o operation,working with files
Managing console i/o operation,working with filesManaging console i/o operation,working with files
Managing console i/o operation,working with files
ramya marichamy
 
Managing,working with files
Managing,working with filesManaging,working with files
Managing,working with files
kirupasuchi1996
 
File handling complete programs in c++
File handling complete programs in c++File handling complete programs in c++
File handling complete programs in c++
zain ul hassan
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
ssuserd6b1fd
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
Rex Joe
 
Files in c++
Files in c++Files in c++
Files in c++
Selvin Josy Bai Somu
 
17 files and streams
17 files and streams17 files and streams
17 files and streams
Docent Education
 
File & Exception Handling in C++.pptx
File & Exception Handling in C++.pptxFile & Exception Handling in C++.pptx
File & Exception Handling in C++.pptx
RutujaTandalwade
 
C
CC
C
MalathiNagarajan20
 
Programming in C
Programming in CProgramming in C
Programming in C
nagathangaraj
 
PPS Notes Unit 5.pdf
PPS Notes Unit 5.pdfPPS Notes Unit 5.pdf
PPS Notes Unit 5.pdf
Sreedhar Chowdam
 
PL-II Lecture19 oop in c++ pyaray bachon .ppt
PL-II Lecture19  oop in c++  pyaray bachon .pptPL-II Lecture19  oop in c++  pyaray bachon .ppt
PL-II Lecture19 oop in c++ pyaray bachon .ppt
inambscs4508
 
ONLINE STUDENT MANAGEMENT SYSTEM
ONLINE STUDENT MANAGEMENT SYSTEMONLINE STUDENT MANAGEMENT SYSTEM
ONLINE STUDENT MANAGEMENT SYSTEM
Rohit malav
 
Introduction to files management systems
Introduction to files management systemsIntroduction to files management systems
Introduction to files management systems
araba8
 
Streaming and input output mOOPlec9.pptx
Streaming and input output mOOPlec9.pptxStreaming and input output mOOPlec9.pptx
Streaming and input output mOOPlec9.pptx
areebakanwal12
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdf
anandatalapatra
 
Managing console i/o operation,working with files
Managing console i/o operation,working with filesManaging console i/o operation,working with files
Managing console i/o operation,working with files
ramya marichamy
 
Managing,working with files
Managing,working with filesManaging,working with files
Managing,working with files
kirupasuchi1996
 
File handling complete programs in c++
File handling complete programs in c++File handling complete programs in c++
File handling complete programs in c++
zain ul hassan
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
ssuserd6b1fd
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
Rex Joe
 
File & Exception Handling in C++.pptx
File & Exception Handling in C++.pptxFile & Exception Handling in C++.pptx
File & Exception Handling in C++.pptx
RutujaTandalwade
 

Recently uploaded (20)

Decision Tree-ID3,C4.5,CART,Regression Tree
Decision Tree-ID3,C4.5,CART,Regression TreeDecision Tree-ID3,C4.5,CART,Regression Tree
Decision Tree-ID3,C4.5,CART,Regression Tree
Global Academy of Technology
 
Quiz-E-Mataram (Under 20 Quiz Set) .pptx
Quiz-E-Mataram (Under 20 Quiz Set) .pptxQuiz-E-Mataram (Under 20 Quiz Set) .pptx
Quiz-E-Mataram (Under 20 Quiz Set) .pptx
SouptikUkil
 
Odoo 18 Point of Sale PWA - Odoo Slides
Odoo 18 Point of Sale PWA  - Odoo  SlidesOdoo 18 Point of Sale PWA  - Odoo  Slides
Odoo 18 Point of Sale PWA - Odoo Slides
Celine George
 
EVALUATION AND MANAGEMENT OF OPEN FRACTURE
EVALUATION AND MANAGEMENT OF OPEN FRACTUREEVALUATION AND MANAGEMENT OF OPEN FRACTURE
EVALUATION AND MANAGEMENT OF OPEN FRACTURE
BipulBorthakur
 
Unit 1 Tools Beneficial for Monitoring the Debugging Process.pdf
Unit 1 Tools Beneficial for Monitoring the Debugging Process.pdfUnit 1 Tools Beneficial for Monitoring the Debugging Process.pdf
Unit 1 Tools Beneficial for Monitoring the Debugging Process.pdf
ChatanBawankar
 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
siemaillard
 
Regression Analysis-Machine Learning -Different Types
Regression Analysis-Machine Learning -Different TypesRegression Analysis-Machine Learning -Different Types
Regression Analysis-Machine Learning -Different Types
Global Academy of Technology
 
Writing Research Papers: Guidance for Research Community
Writing Research Papers: Guidance for Research CommunityWriting Research Papers: Guidance for Research Community
Writing Research Papers: Guidance for Research Community
Rishi Bankim Chandra Evening College, Naihati, North 24 Parganas, West Bengal, India
 
Unit 2 DNS Spoofing in a BadUSB Attack.pdf
Unit 2 DNS Spoofing in a BadUSB Attack.pdfUnit 2 DNS Spoofing in a BadUSB Attack.pdf
Unit 2 DNS Spoofing in a BadUSB Attack.pdf
ChatanBawankar
 
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
Arshad Shaikh
 
Research Handbook On Environment And Investment Law Kate Miles
Research Handbook On Environment And Investment Law Kate MilesResearch Handbook On Environment And Investment Law Kate Miles
Research Handbook On Environment And Investment Law Kate Miles
mucomousamir
 
Sri Guru Arjun Dev Ji .
Sri Guru Arjun Dev Ji                   .Sri Guru Arjun Dev Ji                   .
Sri Guru Arjun Dev Ji .
Balvir Singh
 
Protest - Student Revision Booklet For VCE English
Protest - Student Revision Booklet For VCE EnglishProtest - Student Revision Booklet For VCE English
Protest - Student Revision Booklet For VCE English
jpinnuck
 
"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...
"Dictyoptera: The Order of Cockroaches and Mantises"  Or, more specifically: ..."Dictyoptera: The Order of Cockroaches and Mantises"  Or, more specifically: ...
"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...
Arshad Shaikh
 
Unit 1 Kali NetHunter is the official Kali Linux penetration testing platform...
Unit 1 Kali NetHunter is the official Kali Linux penetration testing platform...Unit 1 Kali NetHunter is the official Kali Linux penetration testing platform...
Unit 1 Kali NetHunter is the official Kali Linux penetration testing platform...
ChatanBawankar
 
The Splitting of the Moon (Shaqq al-Qamar).pdf
The Splitting of the Moon (Shaqq al-Qamar).pdfThe Splitting of the Moon (Shaqq al-Qamar).pdf
The Splitting of the Moon (Shaqq al-Qamar).pdf
Mirza Gazanfar Ali Baig
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-25-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-25-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-25-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-25-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
[2025] Qualtric XM-EX-EXPERT Study Plan | Practice Questions + Exam Details
[2025] Qualtric XM-EX-EXPERT Study Plan | Practice Questions + Exam Details[2025] Qualtric XM-EX-EXPERT Study Plan | Practice Questions + Exam Details
[2025] Qualtric XM-EX-EXPERT Study Plan | Practice Questions + Exam Details
Jenny408767
 
QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptxQUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
Sourav Kr Podder
 
Multicultural approach in education - B.Ed
Multicultural approach in education - B.EdMulticultural approach in education - B.Ed
Multicultural approach in education - B.Ed
prathimagowda443
 
Quiz-E-Mataram (Under 20 Quiz Set) .pptx
Quiz-E-Mataram (Under 20 Quiz Set) .pptxQuiz-E-Mataram (Under 20 Quiz Set) .pptx
Quiz-E-Mataram (Under 20 Quiz Set) .pptx
SouptikUkil
 
Odoo 18 Point of Sale PWA - Odoo Slides
Odoo 18 Point of Sale PWA  - Odoo  SlidesOdoo 18 Point of Sale PWA  - Odoo  Slides
Odoo 18 Point of Sale PWA - Odoo Slides
Celine George
 
EVALUATION AND MANAGEMENT OF OPEN FRACTURE
EVALUATION AND MANAGEMENT OF OPEN FRACTUREEVALUATION AND MANAGEMENT OF OPEN FRACTURE
EVALUATION AND MANAGEMENT OF OPEN FRACTURE
BipulBorthakur
 
Unit 1 Tools Beneficial for Monitoring the Debugging Process.pdf
Unit 1 Tools Beneficial for Monitoring the Debugging Process.pdfUnit 1 Tools Beneficial for Monitoring the Debugging Process.pdf
Unit 1 Tools Beneficial for Monitoring the Debugging Process.pdf
ChatanBawankar
 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
siemaillard
 
Regression Analysis-Machine Learning -Different Types
Regression Analysis-Machine Learning -Different TypesRegression Analysis-Machine Learning -Different Types
Regression Analysis-Machine Learning -Different Types
Global Academy of Technology
 
Unit 2 DNS Spoofing in a BadUSB Attack.pdf
Unit 2 DNS Spoofing in a BadUSB Attack.pdfUnit 2 DNS Spoofing in a BadUSB Attack.pdf
Unit 2 DNS Spoofing in a BadUSB Attack.pdf
ChatanBawankar
 
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
Arshad Shaikh
 
Research Handbook On Environment And Investment Law Kate Miles
Research Handbook On Environment And Investment Law Kate MilesResearch Handbook On Environment And Investment Law Kate Miles
Research Handbook On Environment And Investment Law Kate Miles
mucomousamir
 
Sri Guru Arjun Dev Ji .
Sri Guru Arjun Dev Ji                   .Sri Guru Arjun Dev Ji                   .
Sri Guru Arjun Dev Ji .
Balvir Singh
 
Protest - Student Revision Booklet For VCE English
Protest - Student Revision Booklet For VCE EnglishProtest - Student Revision Booklet For VCE English
Protest - Student Revision Booklet For VCE English
jpinnuck
 
"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...
"Dictyoptera: The Order of Cockroaches and Mantises"  Or, more specifically: ..."Dictyoptera: The Order of Cockroaches and Mantises"  Or, more specifically: ...
"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...
Arshad Shaikh
 
Unit 1 Kali NetHunter is the official Kali Linux penetration testing platform...
Unit 1 Kali NetHunter is the official Kali Linux penetration testing platform...Unit 1 Kali NetHunter is the official Kali Linux penetration testing platform...
Unit 1 Kali NetHunter is the official Kali Linux penetration testing platform...
ChatanBawankar
 
The Splitting of the Moon (Shaqq al-Qamar).pdf
The Splitting of the Moon (Shaqq al-Qamar).pdfThe Splitting of the Moon (Shaqq al-Qamar).pdf
The Splitting of the Moon (Shaqq al-Qamar).pdf
Mirza Gazanfar Ali Baig
 
[2025] Qualtric XM-EX-EXPERT Study Plan | Practice Questions + Exam Details
[2025] Qualtric XM-EX-EXPERT Study Plan | Practice Questions + Exam Details[2025] Qualtric XM-EX-EXPERT Study Plan | Practice Questions + Exam Details
[2025] Qualtric XM-EX-EXPERT Study Plan | Practice Questions + Exam Details
Jenny408767
 
QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptxQUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
QUIZ-O-FORCE PRELIMINARY ANSWER SLIDE.pptx
Sourav Kr Podder
 
Multicultural approach in education - B.Ed
Multicultural approach in education - B.EdMulticultural approach in education - B.Ed
Multicultural approach in education - B.Ed
prathimagowda443
 

File Handling in C++ full ppt slide presentation.ppt

  • 1. Disk File I/O in C++ Dr. Syed Aun Irtaza Department of Computer Science University of Engineering and Technology (UET) Taxila
  • 2. Disk File I/O with Streams O Working with disk files requires set of classes: O ifstream for input, O ofstream for output. O fstream for both input and output O Objects of these classes can be associated with disk files, and we can use their member functions to read and write to the files. O The ifstream, ofstream, and fstream classes are declared in the FSTREAM file.
  • 3. Writing Data formatted I/O, #include<iostream> #include<fstream> #include<string.h> using namespace std; int main() { char ch = 'x'; int j = 77; double d = 6.02; string str1 = "Hello"; string str2 = "Testing"; ofstream outfile("data.txt", ios::out | ios::trunc); //create ofstream object outfile << ch << j << ‘ ' << d << str1 << ‘ ' << str2; cout << "File writtenn"; return 0; }
  • 4. Writing Data formatted I/O, int main() { char ch = ‘x’; int j = 77; double d = 6.02; string str1 = “Hello”; string str2 = “Testing”; ofstream outfile(“data.txt”); //create ofstream object outfile << ch << j << ‘ ‘ << d << str1 << ‘ ‘ << str2; cout << “File writtenn”; return 0; } If the file doesn’t exist, it is created. If it does exist, it is truncated and the new data replaces the old.
  • 5. Reading Data formatted I/O, int main() { char ch; int j; double d; string str1; string str2; ifstream infile(“fdata.txt”); infile >> ch >> j >> d >> str1 >> str2; cout << ch << endl << j << endl << d << endl << str1 << endl << str2 << endl; return 0; }
  • 6. Strings with Embedded Blanks #include <fstream> int main() { ofstream outfile(“TEST.TXT”); outfile << “Hello Dear !n”; outfile << “We are writingn”; outfile << “data in filesn”; outfile << “with embeddedn”; outfile << “blanksn”; return 0; } #include <iostream> #include<fstream> int main() { const int MAX = 80; char buffer[MAX]; ifstream infile(“TEST.TXT”); while( !infile.eof() ) { infile.getline(buffer, MAX); cout << buffer << endl; } return 0; }
  • 7. Character I/O #include <fstream> #include <iostream> #include <string> int main() { string str = “Time is a great teacher, but unfortunately it kills all its pupils. Berlioz”; ofstream outfile(“TEST.TXT”); for(int j=0; j<str.size(); j++) outfile.put( str[j] ); cout << “File writtenn”; return 0; }
  • 8. Character I/O #include <fstream> #include <iostream> void main() { char ch; ifstream infile(“TEST.TXT”); while( infile ){ infile.get(ch); //read character cout << ch; //display it } cout << endl; } read until EOF or error
  • 9. Binary I/O O We can write a few numbers to disk using formatted I/O, but if you’re storing a large amount of numerical data it’s more efficient to use binary I/O O In binary I/O numbers are stored as they are in the computer’s RAM memory, rather than as strings of characters
  • 10. Binary I/O O In binary I/O an int is stored in 4 bytes, whereas its text version might be “12345”, requiring 5 bytes. O Similarly, a float is always stored in 4 bytes, while its formatted version might be “6.02314e13”, requiring 10 bytes.
  • 11. Reading & Writing an Object to Disk struct student{ int rno; char name[25]; }; void getdata(student &s1){ cout<<"nEnter name";cin>>s1.name; cout<<"nEnter Roll no";cin>>s1.rno; } void showdata(student &s2){ cout<<"nName";cout<<s2.name; cout<<"nRoll no"<<s2.rno; }
  • 12. Reading & Writing an Object to Disk int main() { student std[3]; for(int i=0; i<=2; i++) getdata(std[i]); ofstream outfile("std.txt", ios::binary); for(int a=0; a<=2; a++) outfile.write(reinterpret_cast<char*>(&std[a]), sizeof(std[a])); ifstream infile("std.txt", ios::binary); for(int a=0; a<=2; a++){ infile.read( reinterpret_cast<char*>(&std[a]), sizeof(std[a])); showdata(std[a]); } }
  • 14. File pointer positions O Each file object has associated with it two integer values called the get pointer and the put pointer. O These are also called the current get position and the current put position. O These values specify the byte number in the file where writing or reading will take place. O The seekg() and tellg() functions allow you to set and examine the get pointer. O The seekp() and tellp() functions perform these same actions on the put pointer.
  • 15. File pointer positions O For example, will set the put pointer to 10 bytes before the end of the file. O seekp(-10, ios::end); O For example, will set the put pointer to the end of file. O infile.seekg(0, ios::end);