1 / 11

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

FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by Dhimas Ruswanto , BMm. Lecture Overview. Basic File Input Output Output Input. Basic. In C++, as well as any programming language, you can read and write to a file.

Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by DhimasRuswanto, 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 of text files. Does not deal with 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: ofstreamtheoutputfile; • 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 () { ofstreammyfile; //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: ifstreamtheinputfile; • 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 () { stringline; ifstreammyfile ("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

More Related