lab 11 input ouput files

Upload: hafizwadi-othman

Post on 06-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Lab 11 Input Ouput Files

    1/33

    1

    LECTURE 10

    BITP 1113:

    Files and Stream

  • 8/2/2019 Lab 11 Input Ouput Files

    2/33

    2

    LEARNING OUTCOMES

    At the end of this lecture, you should be able to:

    1. Describe the fundamentals of input & output files.

    2. Use data files for input & output purposes.

  • 8/2/2019 Lab 11 Input Ouput Files

    3/33

    3

    Files

    Normally, it is difficult to handle large amounts of inputdata and output data and would always be lost as soonas we turned off the computer .

    We can use a file instead of keyboard for program input.The file is called input file.

    We can use a file instead of monitor screen for programoutput. The file is called output file.

    File a collection of related data usually stored insecondary storage devices such as Disk, Tape, etc.

    Purpose to keep data in a secondary storage.

  • 8/2/2019 Lab 11 Input Ouput Files

    4/33

    4

    Files

    Using file, data can be created by one program, stored(or saved) on secondary devices, and then accessed ormodified by other programs when necessary.

    Two major characteristics of files are:

    Text files : data in text format Binary files : data in binary format

    Files in a secondary storage can be both read andwritten. These processes are also known as input/output.

  • 8/2/2019 Lab 11 Input Ouput Files

    5/33

    5

    Types of File Stream

    The type of stream class determines if it is an input file or

    an output file or both.

    ofstream output file stream class. Creating files andwriting data to them (output file).

    ifstream input file stream class. Opening existing

    and reading data from them into memory (input file).

    fstream file stream class. Creating files, writingdata to them and reading data from them (both input& output file).

  • 8/2/2019 Lab 11 Input Ouput Files

    6/33

    What are the steps needed to Use Files?

    1) Include the header file

    2) Define a file stream object using appropriate file streamclass

    3) Open the file

    4) Use the file

    5) Close the file

    6

  • 8/2/2019 Lab 11 Input Ouput Files

    7/33

    (1)- Include the header file

    Standard input stream(cin) and standard output stream

    (cout) requires the header file. File input/output stream both requires

    header file.

    (2) - Define a file stream object usingappropriate filestream class ( ifstream / ofstream / fstream )

    A file is identified by its file name, e.g. student.dat

    Inside a C++ program, a file is identified by a file stream

    object.eg:ifstreaminFile; // inFile is an input file stream

    // object, with ifstream class

    eg:ofstreamoutFile;// outFile is an output file// stream object, with ofstream class

    7

  • 8/2/2019 Lab 11 Input Ouput Files

    8/33

  • 8/2/2019 Lab 11 Input Ouput Files

    9/33

    9

    Input / Output : File Name vs File Stream Object

    inFile

    outFile

    student.dat

    student_new.dat

    ifstream inFile(student.dat);ofstream outFile(student_new.dat);

    Memory

  • 8/2/2019 Lab 11 Input Ouput Files

    10/33

    10

    File Name

    Example:

    ifstream myInputFile;char fileName[20];

    cout> fileName;myInputFile.open(fileName);

    OR

    char fileName[20];

    cout> fileName;

    ifstream inFile(fileName);

  • 8/2/2019 Lab 11 Input Ouput Files

    11/33

    There are a few modes to open a file. They are listed asfollows:Mode Description

    ios::in Input mode. Data will be read from the file. Thefile MUST exists. If the file doesnt exist, it will not be created

    and the open function fails. ios::out Output mode. Data will be written to the file.

    The file will be created if it does not exist. If it already exists,the files content will be deleted and replaced by the new data.

    ios::trunc Truncate mode. Same as ios::out.

    ios::appAppend mode. If the file already exists, itscontents are preserved and all output is written to the end of

    the file. If the file doesnt exist, it will be created.

    Open File Modes

    11

  • 8/2/2019 Lab 11 Input Ouput Files

    12/33

    All these modes can be combined using the | operator(which means OR) .

    For example, if we want to open the file example.binto add data we could do it by the following :

    ofstream myfile("example.bin", ios::out | ios::app);

    More on Open File Mode

    12

  • 8/2/2019 Lab 11 Input Ouput Files

    13/33

    For all file stream classes, default mode/s is/areautomatically applied if the file is opened withoutspecifying any value for the mode parameter.

    Stream class Default mode parameter

    ofstream ios::out

    ifstream ios::in

    fstream ios::in | ios::out

    More on Open File Mode

    13

  • 8/2/2019 Lab 11 Input Ouput Files

    14/33

    (4) - Use the file

    Can use output file stream object andto copy data

    from file to variablesinFile >> partNum;inFile >> qtyInStock >> qtyOnOrder;

    14

  • 8/2/2019 Lab 11 Input Ouput Files

    15/33

    15

    (5) - Closing a File

    When we are finished with our input and outputoperations on a file we should close it.

    When a file is closed, any unsaved data in the buffer willbe saved to its file.

    Closing a file improves systems performance (theoperating systems resources is better utilized.)

    Use the close member function

    inFile.close();

    outFile.close(); After closing a file, the file stream object (above eg:inFile and outFile) can be used again to openanother file.

  • 8/2/2019 Lab 11 Input Ouput Files

    16/33

    Example of Output File

    Writing output to a text file : Example 1

    # include

    # include using namespace std;

    int main()

    {

    double income = 123.45, expenses = 987.65;

    int week = 7, year = 2006;

    ofstream outfile("L4_2.out");// default mode is ios::out

    outfile

  • 8/2/2019 Lab 11 Input Ouput Files

    17/33

    Example of Output File: More on Example 1

    Content of L4_2.out text file :

    You can find the L4_2.out file in the same folder asthe C++ program that wrote the file. Open it using thenotepad.

    17

  • 8/2/2019 Lab 11 Input Ouput Files

    18/33

    Writing output to a text file : Example 2

    #include

    #include

    #include

    using namespace std;

    int main (){

    char name[20];

    char id[10];

    float m1,m2;

    ofstream dataFile (student.txt", ios::out);

    cout> name >> m1 >> m2) //reading from keyboard using EOF while loop

    {

    dataFile

  • 8/2/2019 Lab 11 Input Ouput Files

    19/33

    Example of Output File : More on Example 2

    For example the input of the program is entered asfollows :

    19

  • 8/2/2019 Lab 11 Input Ouput Files

    20/33

    Example of Output File : More on Example 2

    Content of student.txt text file :

    You can find the student.txt file in the samefolder as the C++ program that wrote the file. Open itusing the notepad.

    20

  • 8/2/2019 Lab 11 Input Ouput Files

    21/33

    Input files can be also created using any text editor.Notepad is an example of text editor you can use to saveyour data.

    To create an input file using a text editor, open thenotepad and type the data. Save the file in the samefolder with the C++ program that will use these data.Example of the file is shown below:

    Input Files

    Save the file as : L4_3A.dat

    21

  • 8/2/2019 Lab 11 Input Ouput Files

    22/33

    Reading data from a text file: Example 1# include # include #include

    using namespace std;

    int main()

    {

    double x;

    int i,j;

    ifstream infile1("L4_3A.dat");ifstream infile2("L4_3B.dat");

    infile1>>i>>j>>x;

    infile1.close();cout

  • 8/2/2019 Lab 11 Input Ouput Files

    23/33

    Reading data from a text file: Example 2#include

    #include

    #include

    using namespace std;

    int main ()

    { char line[50];ifstream myFile ("example.txt");

    if (myFile.is_open()) //checking file existance

    { while (! myFile.eof() ) // checking EOF for myFile using while loop

    { myFile.getline (line,50); //read string from myFile

    cout

  • 8/2/2019 Lab 11 Input Ouput Files

    24/33

  • 8/2/2019 Lab 11 Input Ouput Files

    25/33

    Reading data from a text file: Example 3#include

    #include

    #include

    using namespace std;

    int main ()

    { char name[20];

    char id[10];

    float m1,m2;

    ifstream dataFile (student.txt", ios::in);

    if (!dataFile) // if dataFile exists, dataFile expression is TRUE

    {

    cout > id >> name >> m1 >> m2) //read from dataFile using EOF while loop

    {

    cout

  • 8/2/2019 Lab 11 Input Ouput Files

    26/33

    26

    Example: Writing Data to a File#include

    #include

    using namespace std;

    voidmain()

    {

    ofstream myOutputFile; // declaring output file object

    myOutputFile.open(student_new.dat); // file created in// the same directory as the program

    myOutputFile

  • 8/2/2019 Lab 11 Input Ouput Files

    27/33

    27

    Example: Reading Data From a File#include#include

    using namespace std;

    voidmain(){ifstream myInputFile; // declaring input file objectchar name[50];

    myInputFile.open(student_new.dat); // file will be// created in the same directory as the program

    myInputFile >> name;cout name;cout name;cout

  • 8/2/2019 Lab 11 Input Ouput Files

    28/33

    28

  • 8/2/2019 Lab 11 Input Ouput Files

    29/33

    29

    #include #include #include //required to use exit()#include using namespace std;

    voidmain(){char time[5];double temperature;

    ifstream inFile("temperature.txt",ios::in);

    ofstream outFile("temperature_revised.txt",ios::out);

    if (!inFile) // check existence of input file{ cout >temperature;outFile

  • 8/2/2019 Lab 11 Input Ouput Files

    30/33

    30

  • 8/2/2019 Lab 11 Input Ouput Files

    31/33

    31

    Open the input file

    ifstream inFile;

    inFile.open(clients.txt",ios::in);OR

    ifstream inFile(clients.txt",ios::in);

    Open the output file

    ofstream outFile;outFile.open("output.txt",ios::out);

    OR

    ofstream outFile("output.txt",ios::out);

    Append the output file

    ofstream outFile;outFile.open("output.txt",ios::app);

    OR

    ofstream outFile("output.txt",ios::app);

  • 8/2/2019 Lab 11 Input Ouput Files

    32/33

    32

    Passing File Stream Objects to Functions#include#includeusing namespace std;

    const int SIZE = 5; // maximum size of charactervoidshowContent (ifstream&); // function prototype

    void main(){

    ifstream inFile;

    inFile.open("temperature.txt");

    if ( !inFile){

    cout

  • 8/2/2019 Lab 11 Input Ouput Files

    33/33

    33

    voidshowContent (ifstream &inFileObj){

    char time[SIZE];double temperature;

    while (!inFileObj.eof()){

    inFileObj >> time >> temperature; // read content

    cout