The Benefits and Challenges of Open Educational Resources
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.
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.
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);