Download presentation
Presentation is loading. Please wait.
1
File Handling Accessing a data file for input: 1) Include the file stream header file: #include using namespace std; 2) Associate an input file name with an input file stream for accessing the data file. ifstream fin(“datafile.in”); You may explicitly indicate the mode of file access as input: ifstream fin(“datafile.in”,ios::in); 1
2
File Handling 3) Use the associated file name to access the data: fin>>accountNum>>balance; or while(fin>>accountNum[i]>>balance[i]){ … } to retrieve data until end of file (EOF) is reached 4) Close the file indicating that the data access is terminated. fin.close( ); 2
3
File Handling Accessing a data file for output: 1) Include the file stream header file: #include using namespace std; 2) Associate an output file name with an output file stream: ofstream fout(“datafile.out”,ios::out); 3
4
File Handling 3) Use the associated file name to output the data: fout<<accountNum<< “ ”<<balance<<endl; 4) Close the file indicating that the data access is terminated. fout.close( ); 4
5
Mode of Opening a File Input –ifstream fin(“datafile.in”, ios::in); Output –ofstream fout(“datafile.out”, ios::out); Both input and output –fstream acctFile(“accountfile.txt”, ios::in|ios::out); Append –ofstream fout(“accountfile.txt”, ios::app); 5
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.