File I/O
Files allow permanent storage of programs and data. ifstream and ofstream objects –For file I/O the inclusion of is required. –Objects for file I/O must be created: For file output a variable/object of type ofstream will be created and will be used like cout For file input a variable/object of type ifstream will be created and will be used like cin –Any valid variable name may be used for the variables. Two Names Involved in File I/O –There are two names that must be distinguished between: 1.variable name - the name used to access the file object in the program 2.filename - the name the file is known by in the operating system
Opening and Closing Files Opening a file connects the file with specified filename to an input or output stream (variable), if successful –Opening a file causes a request to be made to the operating system to open the file –The operating system is in charge of files and does things like checking access permissions setting up a data structure for the opened file that includes information such as the location for the next read or write Closing the file object terminates access to the file by the program and causes the operating system "disconnect" it from the program.
Opening and Closing Files in a Program When an ifstream or an ofstream object is defined filename may be specified and then the file will be opened (if there is no error) if no filename is specified then file opening must be done as a separate step Opening a file fails if: file does not exist or program does not have permission to access the file It is necessary to check if it the file was open successfully
Defining fstream objects: 2-step example input/output stream needs to be declared: ifstream in_stream; // input stream ofstream out_stream; // output stream opening the file: in_stream.open(”myfile.dat”); // opening file -before C++ program can read from or write to a file stream, the stream needs to be connected to a file Note: string variable can be used for file name ( c_str() function has to be applied on the C++ string) //2-step example string filename=“myfile.dat”; ifstream in_stream; in_stream.open(filename.c_str());
Working with fstream objects after stream variable has been connected to a file, extraction and insertion operators can be used with a stream: in_stream >> var1 >> var2 >> var3; or out_stream << var1 << var2 << var3; after opening the file all the operations on the file is done through associated stream before program finishes every stream needs to closed: in_stream.close(); // closing file
I/O Error Checking Function fail() returns true if previous stream operation failed useful to check if file is opened successfully before performing operations on file Function exit() - exists program immediately To use exit, include exit() accepts one integer as argument: –convention: 0 - normal program termination; nonzero - abnormal Example in_stream.open(“myfile.dat”); // opening file if (in_stream.fail()){ cout << “Cannot open file myfile.dat”; exit(1); }
Defining ifstream object: 1-step example Again, in may be used in the same manner cin is used.
Defining ofstream object – 1-step example Again, out may be used in the same manner cout is used.
Inputting and Outputting Inputting The >> operator reads a whitespace delimited sequence of built-in types and objects (if >> has been defined for the object) Any of the built-in types may be read in using >> –also C-style strings, C++ strings, and complex numbers Two general conditions under which an input stream evaluates to false: –The end-of-file is read. The end-of-file is indicated internally in an operating system dependent manner –An invalid character is encountered. In this case the input stream goes into a state of error and all reading of input ceases Outputting Outputting may be done in the usual manner with the << operator.
Stream Objects as Parameters Streams must be passed to functions by-reference so that a copy is not made If output is done with the reference parameter –the same stream is used in the called function If a copy is made (pass-by-value) –the stream in the calling function does not change when the called function does I/O –called function uses the copy and the original stream (in the calling function) will probably not work correctly afterwards