> num; while ( !indata.eof() ) { cout << "The next number is " << num << endl; indata >> num; // sets EOF flag if no value found } indata.close(); cout << "End-of-file reached.." << endl; }"> > num; while ( !indata.eof() ) { cout << "The next number is " << num << endl; indata >> num; // sets EOF flag if no value found } indata.close(); cout << "End-of-file reached.." << endl; }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Skills2 for Scientific Colleges 1 File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.

Similar presentations


Presentation on theme: "Computer Skills2 for Scientific Colleges 1 File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed."— Presentation transcript:

1 Computer Skills2 for Scientific Colleges 1 File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed

2 Computer Skills2 for Scientific Colleges 2 READING DATA FROM FILES USING C++ The commands for reading data from a file are (for the most part) very similar to those used to read data from the keyboard. In order for your program to read from the file, you must:  include the fstream header file  declare a variable of type ifstream  open the file  read from the file  after each read, check for end-of-file using the eof() member function  close the file when access is no longer needed (optional, but a good practice)

3 Computer Skills2 for Scientific Colleges 3 READING DATA FROM FILES USING C++ #include using namespace std; void main() { ifstream indata; // indata is like cin int num; // variable for input value indata.open("example.txt"); indata >> num; while ( !indata.eof() ) { cout << "The next number is " << num << endl; indata >> num; // sets EOF flag if no value found } indata.close(); cout << "End-of-file reached.." << endl; }

4 Computer Skills2 for Scientific Colleges READING DATA FROM FILES USING C++ The result:

5 Computer Skills2 for Scientific Colleges 5 READING DATA FROM FILES USING C++ #include using namespace std; void main() { ifstream indata; // indata is like cin char letter; // variable for input value indata.open("example.txt"); // opens the file indata >> letter; while ( letter != 'Q' ) { cout << letter; indata >> letter; } cout << endl; indata.close(); cout << "End-of-file reached.." << endl; }

6 Computer Skills2 for Scientific Colleges READING DATA FROM FILES USING C++ The result:

7 Computer Skills2 for Scientific Colleges WRITING DATA IN THE FILES USING C++ In order for your program to writein the file, you must:  include the fstream header file  declare a variable of type ofstream  open the file  Writ in the file  close the file when access is no longer needed (optional, but a good practice)

8 WRITING DATA IN THE FILES USING C++ #include using namespace std; void main () { ofstream myfile; myfile.open("example.dat"); myfile << "Writing this to a file.\n"; } Computer Skills2 for Scientific Colleges 8

9 9 WRITING DATA IN THE FILES USING C++

10 Computer Skills2 for Scientific Colleges Input/Output with files #include using namespace std; void main(){ // To write in a file: ofstream obj_out("file_05.txt"); int x; while(cin >> x) { obj_out << x << endl; }// To stop writing to a file, press CTRL+Z then ENTER // To read from a file: ifstream obj_in("file_05.txt"); while(obj_in >> x) cout << "X from the file is: " << x << endl; cout<<endl; }

11 Computer Skills2 for Scientific Colleges 11 Input/Output with files

12 Computer Skills2 for Scientific Colleges 12 Example Q1: Write a program that copies a content of a file into a one-dimensional array. Assume that the content of the file is integers.

13 Computer Skills2 for Scientific Colleges 13 Solution #include using namespace std; void main() { int x, c=0; ifstream fin1("d:\\test.txt", ios::in); while(fin1>>x) c++; ifstream fin2("d:\\test.txt", ios::in); int *a = new int[c]; for(int i=0; i<c; i++) fin2 >> a[i]; // To print the content of the array for(int i=0; i<c; i++) cout << a[i] << endl; }

14 14 Example Q2: Write a program that copies a content of a file into a one-dimensional array. Assume that the content of the file is string.

15 Solution #include using namespace std; void main() { string x; int c=0; ifstream fin1("test.txt", ios::in); while(fin1>>x) c++; fin1.close(); ifstream fin2("test.txt", ios::in); string *a = new string[c]; for(int i=0; i<c; i++) fin2 >> a[i]; // To print the content of the array for(int i=0; i<c; i++) cout << a[i] << endl; } Computer Skills2 for Scientific Colleges 15


Download ppt "Computer Skills2 for Scientific Colleges 1 File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed."

Similar presentations


Ads by Google