1 / 13

Streams and Basic File IO

Streams and Basic File IO. Terms to Know. Stream Input Stream Output Stream Reading Writing ifstream ofstream fstream. Connecting to a file External file name Close Object Member function Class Dot operator Calling object fail. Basic IO Terms. A stream is a flow of data

emerald-orr
Download Presentation

Streams and Basic File IO

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. Streams and Basic File IO

  2. Terms to Know • Stream • Input Stream • Output Stream • Reading • Writing • ifstream • ofstream • fstream • Connecting to a file • External file name • Close • Object • Member function • Class • Dot operator • Calling object • fail

  3. Basic IO Terms • A stream is a flow of data • If the flow is into your program, the stream is called an input stream • If the flow is out of your program, the stream is called an output stream • Describe the streams that we have previously used

  4. Why Use Files for IO? • Permanent Storage • Ability to re-use input • Convenient for large quantities of data

  5. Basic FILE IO Terms • When your program takes input from a file, it is said to be reading from the file • When your program sends output to a file, it is said to be writing to the file • The method we use to read a file reads from beginning to end; you are not allowed to back up and read anything in the file a second time • Note: this is exactly how we read input from the keyboard • The same goes for writing to a file

  6. Basic FILE IO Terms (cont.) • A stream is an object variable • To change the value of a stream variable (re-assign), we could disconnect a stream from one file and reconnect to another • You can’t reassign a stream using the assignment operator(=) like you can with a primitive data type • To allocate memory for a stream object, you must declare it • The type for input file stream variables is ifstream • The type for output file stream variables is ofstream • These types are in the library fstream • See Example 1: BasicFileIO

  7. Basic FILE IO Terms (cont.) • Stream variables must be connected to a file • This is called opening a file • Use the open function to connect a stream variable to a file (See example) • This is done with both ifstream and ofstream objects • Once a stream is connected to a file, the extraction and insertion operators can be used in the same way as cin and cout • Note: When using open() with an ofstream, the function will create a new file if one does not already exist, and will empty the contents of files that do already exist • Every file should be closed when your program is finished reading from it or writing to it; a file is closed using the close() function

  8. File Names • Note that the file name given as the argument to the open() funtion must be enclosed in quotations and include the file extension • This is called the external file name • This is the name used by the operating system • Once a stream is connected to a file, you will no longer refer to it by its external file name; you will use the name of the stream to refer to the file

  9. Classes and Objects • An object is a variable that has functions as well as data associated with it • A function that is associated with an object is called a member function • A type whose variables are of type object is a class • A function is called using the dot operator • The object named before the dot is referred to as the calling object

  10. Member Functions in ifstream and ofstream • open() • fail()

  11. Formatting techniques: member functions used for formatting • setf: means to set flag • unsetf • precision • width

  12. Manipulator Functions • A manipulator function is a function that is called in a non-traditional way • Manipulators are placed after an insertion operator • What manipulator function have we already worked with? • setw – manipulator that does the exact same thing as width() • setprecision – manipulator that does the exact same thing as precision() • To use either of these functions, you must include the iomanip library

  13. Formatting Flags for setf

More Related