Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 File Processing in C++ C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on files ifstream: Stream class to read from files fstream: Stream class to both read and write from/to files.

3 Open a file Each one of the open() member functions of the classes ofstream, ifstream and fstream has a default mode that is used if the file is opened without a second argument: class default mode parameter ofstream ios::out ifstream ios::in fstream ios::in | ios::out

4 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.

5 Reading data from files using C++
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)

6 Example Q1: Write a program that reads integer numbers from the following file and print the result in output screen.

7 Reading data from files using C++
#include<iostream> #include <fstream> using namespace std; int main() { ifstream indata; // indata is like cin int num; // variable for input value indata.open(“example.txt",ios::in); indata >> num; while ( !indata.eof() ) { cout << "The next number is " << num << endl; } indata.close(); cout << "End-of-file reached.." << endl;

8 Reading data from files using C++
The result:

9 Example Q2: Write a program that reads characters from the following file as long as the character not Q. and print the result in output screen.

10 Reading data from files using C++
#include<iostream> #include <fstream> using namespace std; void main() { ifstream indata; // indata is like cin char letter; // variable for input value indata.open("example.txt“,ios::in); // opens the file indata >> letter; while ( letter != 'Q' ) { cout << letter; } cout << endl; indata.close(); cout << "End-of-file reached.." << endl;

11 Reading data from files using C++
The result:

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

13 Writing data in the files using C++
#include <iostream> #include <fstream> using namespace std; void main () { ofstream myfile; myfile.open("test1.txt",ios::out); myfile << "Writing this to a file......\n"; myfile.close(); }

14 Writing data in the files using C++

15 Example Q3: Write a program that read 4integer numbers from the user and write them in a file .then read the numbers from a file and print them in output screen.

16 Input/Output with files
#include <iostream> #include <fstream> using namespace std; void main(){ ofstream obj_out("file_05.txt",ios::out); int x; for(int i=0;i<=3;i++) { cin >> x; obj_out << x << endl; } ifstream obj_in("file_05.txt",ios::in); obj_in >> x; while(!obj_in.eof()) cout << "X from the file is: " << x << endl;

17

18 Input/Output with files
#include <iostream> #include <fstream> using namespace std; void main(){ // To write in a file: ofstream obj_out("file_02.txt",ios::out); 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_02.txt",ios::in); while(obj_in >> x) cout << "X from the file is: " << x << endl; }

19 Input/Output with files

20 Example Q: Write a program that copies a content of a file into a one-dimensional array. Assume that the content of the file is integers.

21 Solution #include<iostream> #include<fstream>
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 cout << a[i] << endl; }

22 We can use the same variable from type fstream for input and output in the same time as the following example.

23 Input/Output with files
#include <iostream> #include <fstream> using namespace std; void main(){ fstream obj("file_05.txt"); int x; for(int i=0;i<=3;i++) { cin >> x; obj << x << endl; } obj.close(); obj.open("file_05.txt"); obj>> x; while(!obj.eof()) cout << "X from the file is: " << x << endl;

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

25 Solution #include<iostream> #include<fstream> #include<string> using namespace std; void main() { string x; int c=0; ifstream fin1("test.txt"); while(fin1>>x) c++; fin1.close(); ifstream fin2("test.txt“); //note: we can use the same variable fin1.open("test.txt“); string *a = new string[c]; for(int i=0; i<c; i++) fin2 >> a[i]; // To print the content of the array cout << a[i] << endl; }


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

Similar presentations


Ads by Google