csi 201 introduction to
play

CSI 201 - Introduction to double interest_rate = .059; Types such - PowerPoint PPT Presentation

Introduction Up to now, we've known a variable to be a named reference to a place in memory that stores one type of data value. CSI 201 - Introduction to double interest_rate = .059; Types such as double , char , bool , int , etc. are


  1. Introduction Up to now, we've known a variable to be a named reference to a � place in memory that stores one type of data value. CSI 201 - Introduction to double interest_rate = .059; � Types such as double , char , bool , int , etc. are simple data types . � They are types built into the C++ language. Computer Science An object is a special kind of variable. � An object has: � data (sometimes referred to attributes , members , or member � fields , or fields ) member functions – special functions that are designed to work with � the data contained in the object. Chapter 5 Types for objects are complex data types , called classes . They are � made available through include directives and other means we'll I/O Streams as an Introduction to Objects and Classes discover in Chapter 6. Chapter 6 will go into objects and classes in much more detail. � Brian R. King The goal of this chapter is to simply introduce you to the � Instructor concepts of objects and classes using the I/O Stream library. 2/26/2006 2 CSI 201 -- Chapter 05 Streams - Review File I/O � Why use files? � A stream is a flow of data to and from your program. � Store data permanently � C++ represents streams in your program as objects . � Repeated input data � Input is delivered to your program via an input stream object No need to repeatedly type your data in � � The object is connected to the resource you want to read from. � Allows for large data set processing � How do we use files in our C++ programs? cin is an input stream object connected to the keyboard. � � C++ programs read and write data residing in files through file � Type (or class ) – istream stream objects. � We say that cin is an instance of the class istream . � File stream objects behave much like cin and cout . � Output is sent to an output device via an output stream object � It will be our job to connect the file stream object to an actual file managed by the OS's file system. � The object is connected to the resource you want to send data to. � Terminology: cout is an output stream object connected to the screen. � � When a program takes data from a file, we say the program is � Type (or class ) – ostream reading from the file. � We say that cout is an instance of the class ostream . � When a program sends data to a file, we say the program is writing to the file. 2/26/2006 CSI 201 -- Chapter 05 3 2/26/2006 CSI 201 -- Chapter 05 4

  2. Declaring our own stream variables How do we use a file stream object? Recall, variables of simple data types (e.g. double , short , char , � � Objects have many actions and services available int , etc.) must be declared before they can be used, and that allow us to do things with the objects. initialized before it contains valid data. An object… � � For file stream objects, some possible actions: Must also be declared (sometimes called instantiated in OOP) � Must also be initialized before it contains valid data � Opening a file to attach to the object? � For file stream objects, initialization means connecting the object to � � Testing for a failure incase the file couldn't be opened? an actual file residing in the file system. Types whose variables are objects are called classes . � � Closing the file when we’re done with the file? ifstream - The type for an input-file stream object. � � Changing the format of data being sent to the file stream? ofstream - The type for an output-file stream object. � These types are defined for you by using the include directive: � #include <fstream> They are defined in the std namespace: � � How are these actions and services invoked on the using namespace std; Example declarations: object? � ifstream in_file; � � Member function calls . ofstream out_file; � 2/26/2006 5 2/26/2006 6 CSI 201 -- Chapter 05 CSI 201 -- Chapter 05 Member Function Call Syntax Connecting a file to the stream: � After declaring a file stream object, it still needs to object.member_func(arg1, … ,argn); be connected to a file. � The primary difference between a regular � open - Our first example of a member function : function call and a member function call is the � It takes one argument – a string containing the name of the use of the " dot operator ". file to be opened. � The left of a dot operator contains the name of an � ifstream in_file; in_file.open("input.dat"); object. Interpret this as sending a request to object in_file to open � � The right of a dot operator contains the function the file input.dat and attach it to it's input stream. call that will accomplish the desired service � ofstream out_file; provided by the object. out_file.open("output.dat"); � The right of the dot operator looks like a standard function call as seen in Chapter 3-4. 2/26/2006 CSI 201 -- Chapter 05 7 2/26/2006 CSI 201 -- Chapter 05 8

  3. After the file has opened… Closing the file when completed ifstream in_file; � After you are finished using the file, you should always close the file. ofstream out_file; � This releases the resource back to the operating system. � Use the member function close on the file stream in_file.open("input.dat"); object. out_file.open("output.dat"); � in_file.close(); � out_file.close(); � Now, in_file can be used to read data from the � What are reasons why we should close a file after file input.dat just like cin is used to read data from our program is finished with the file? the keyboard. � out_file can be used to write data to the file output.dat just like cout is used to write to the screen. 2/26/2006 9 2/26/2006 10 CSI 201 -- Chapter 05 CSI 201 -- Chapter 05 ex1.cpp Input file open details #include <iostream> � in_file.open("input.dat"); #include <fstream> � The postconditions of this function call depend on whether the file using namespace std; input.dat can be successfully opened. int main() � If so, it will be opened, ready for reading. { ifstream in_file; � If not, an internal success/fail flag maintained in the ofstream out_file; in_file object will be set to true . in_file.open("input.dat"); � fail() member function - Test the state of the failure flag, thus out_file.open("output.dat"); allowing us to test whether the open function succeeded. double d1, d2, d3; � in_file.fail() returns a type of bool . � true - the open failed in_file >> d1 >> d2 >> d3; out_file << "Average: " << (d1 + d2 + d3) / 3.0 << endl; � false - the open succeeded. � Reasons for input files not opening successfully: in_file.close(); out_file.close(); cout << "Program completed." << endl; return 0; } 2/26/2006 CSI 201 -- Chapter 05 11 2/26/2006 CSI 201 -- Chapter 05 12

  4. Output file open details Append to a file (output stream only) � out_file.open("output.dat"); � The open member function will overwrite the � Creates the file, assuming a valid filename was given as an file if the file already exists. argument to the function. � fail() member function – Test the success of the open � If you want to append data to a file so that function call. the data you write goes after any existing � out_file.fail() behaves the same way as we saw on input stream objects. contents of the file, open the file using a � NOTE - If the file already existed on the file system, then the second argument ios::app in the open open member function call above will cause the file to be overwritten! function call: (There is a work around for this, next slide…) � � out_file.open("output.dat", ios::app ); � Reasons for output files not opening successfully: 2/26/2006 13 2/26/2006 14 CSI 201 -- Chapter 05 CSI 201 -- Chapter 05 If the file fails to open… ex2.cpp – Using fail and exit #include <iostream> When a stream open function fails, sometimes it's best to stop � #include <fstream> execution of the program. #include <cstdlib> using namespace std; The C++ standard library provides such a function: � int main() exit( retValue ); { ifstream in_file; ofstream out_file; It is NOT a member function of any object. � in_file.open("input.dat"); retValue represents any integer value that will be passed back to � if (in_file.fail()) the operating system. { cout << "Input file open failed!\n"; Behaves similarly to the return statement in your main function. � exit(1); Programmers have standardized on using an exit value of 0 to mean } � a successful exit, and a non-zero (usually 1) to mean an error out_file.open("output.dat"); occurred in your program. if (out_file.fail()) { In order to use the exit function, you must have the following � cout << "Output file open failed!\n"; directives: exit(1); #include <cstdlib> } using namespace std; 2/26/2006 CSI 201 -- Chapter 05 15 2/26/2006 CSI 201 -- Chapter 05 16

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend