Presentation is loading. Please wait.

Presentation is loading. Please wait.

File Handling Accessing a data file for input: 1) Include the file stream header file: #include <fstream> using namespace std; 2) Associate an input file.

Similar presentations


Presentation on theme: "File Handling Accessing a data file for input: 1) Include the file stream header file: #include <fstream> using namespace std; 2) Associate an input file."— Presentation transcript:

1 File Handling Accessing a data file for input: 1) Include the file stream header file: #include <fstream> 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);

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( );

3 File Handling Accessing a data file for output: 1) Include the file stream header file: #include <fstream> using namespace std; 2) Associate an output file name with an output file stream: ofstream fout(“datafile.out”,ios::out);

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( );

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);


Download ppt "File Handling Accessing a data file for input: 1) Include the file stream header file: #include <fstream> using namespace std; 2) Associate an input file."

Similar presentations


Ads by Google