Presentation is loading. Please wait.

Presentation is loading. Please wait.

FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by

Similar presentations


Presentation on theme: "FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by"— Presentation transcript:

1 FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by
Dhimas Ruswanto, BMm.

2 Lecture Overview Basic File Input Output Output Input

3 Basic In C++, as well as any programming language, you can read and write to a file. This is done in a similar way to the input/output stream in terms of the library to include with the program. C++ provides 3 of them:

4 Basic #include <ifstream> Library to specifically deal with input of text files. Does not deal with output. #include <ofstream> Library to specifically deal with output input. #include <fstream> Library to deal with BOTH input and output.

5 Output To declare the output file stream i.e. for writing we would declare like this: ofstream  theoutputfile; For writing, to connect to a stream, let say opening file testfileio.dat for writing, the file which located in the same folder as the running program.  Previous content of the testfileio.dat will be overwritten, we could write like this: theoutputfile.open("testfileio.dat"); Or with the path: theoutputfile.open("c:\\testfileio.dat "); After we have completed the file processing, we have to close or disconnect the stream to free up the resources to be used by other processes or programs.  Using the close() member function, for output stream: theoutputfile.close();

6 Output #include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; //declare the variable of type ofstream since you are dealing with output: myfile.open ("example.txt"); //function to open the file which includes the file name: if(myfile.is_open()) //check if the file is open with the is_open() function: myfile << "Hello world! This is output!" << endl; //perform the operation(s): myfile.close(); //function to close the file: }else{ //is_open() returned false and there is a problem: cout << "Can't open the file!" << endl; } return 0;

7 Output open() The open() function will simply open a file for either input or output. The above program did it for output. The parameter of the function will be the file name. If the file does not exist in the directory, C++ will create it for you. close() A simple function designed to close the file and it's stream. It will require no parameters. is_open() The is_open() function is a boolean function that will check whether or not a file is open. If the function returns true, the file is open without any problems. If it returns false, the file is not good and therefore cannot be used.

8 Input To declare the input file stream  i.e. for reading we would declare like this: ifstream  theinputfile; Then, to connect to a stream, let say opening file testfileio.dat for reading, the file which located in the same folder as the executable program we would write like this: theinputfile.open("testfileio.dat "); Or with the path we could write: theinputfile.open("c:\\testfileio.dat "); After we have completed the file processing, we have to close or disconnect the stream to free up the resources to be used by other processes or programs.  Using the close() member function, for input stream we would write like this: theinputfile.close();

9 Input #include <iostream> #include <fstream> #include <string> using namespace std; int main () { string line; ifstream myfile ("example.txt"); //the variable of type ifstream: if (myfile.is_open()) //check to see if the file is opened: { while (! myfile.eof() ) //while there are still lines in the file, keep reading: getline (myfile,line); //place the line from myfile into the line variable: cout << line << endl; //display the line we gathered: } myfile.close(); //close the stream: else cout << "Unable to open file"; return 0; }

10 Input eof() The eof() function is a boolean function that will check whether or not the file has reached the end. It returns true when the file is at the end and false otherwise.

11 SUMMARY ! DO and SUBMIT your ASSIGNMENTS


Download ppt "FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by"

Similar presentations


Ads by Google