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

muhazamali0 9 views 16 slides May 08, 2024
Slide 1
Slide 1 of 16
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16

About This Presentation

This is a file handling in c++.


Slide Content

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 Working with disk files requires set of classes: ifstream for input, ofstream for output. fstream for both input and output Objects of these classes can be associated with disk files, and we can use their member functions to read and write to the files. 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 written\n"; 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 written\n”; 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 writing\n”; outfile << “data in files\n”; outfile << “with embedded\n”; outfile << “blanks\n”; 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 written\n”; 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 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 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 In binary I/O an int is stored in 4 bytes, whereas its text version might be “12345”, requiring 5 bytes. 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 Each file object has associated with it two integer values called the get pointer and the put pointer . These are also called the current get position and the current put position . These values specify the byte number in the file where writing or reading will take place. The seekg () and tellg () functions allow you to set and examine the get pointer. The seekp () and tellp () functions perform these same actions on the put pointer.

File pointer positions For example, will set the put pointer to 10 bytes before the end of the file. seekp (-10, ios ::end); For example, will set the put pointer to the end of file. infile.seekg (0, ios ::end);

File pointer positions
Tags