Understanding Basic File I/O in C++: Streams and Concepts
Explore the fundamental concepts of File I/O in C++ including the definition and usage of streams. Learn the roles of input and output streams, how to read from and write to files, and the importance of accessing external file names. Understand the classes and member functions used for file manipulation, including ifstream, ofstream, and fstream. This guide covers how to connect to files, close them properly, and utilize formatting techniques with member functions and manipulators. Ideal for beginners looking to grasp the basics of file handling in C++.
Understanding Basic File I/O in C++: Streams and Concepts
E N D
Presentation Transcript
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 • 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
Why Use Files for IO? • Permanent Storage • Ability to re-use input • Convenient for large quantities of data
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
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
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
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
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
Member Functions in ifstream and ofstream • open() • fail()
Formatting techniques: member functions used for formatting • setf: means to set flag • unsetf • precision • width
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