File Handling in C++ full ppt slide presentation.ppt
muhazamali0
9 views
16 slides
May 08, 2024
Slide 1 of 16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
About This Presentation
This is a file handling in c++.
Size: 910.62 KB
Language: en
Added: May 08, 2024
Slides: 16 pages
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, 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.
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);