Program Input and the Software Design Process ROBERT REAVES File Input and Output File is a named area in a secondary storage that holds a collection of information. Why would we want to read from a file instead of a keyboard? Little mistakes. Easier to enter in large amounts of data multiple times. Why would we want to write to a file? Allows us to look at the output over and over without a rerun. Output of one program can be used as input to another. Using Files If we want file I/O in program we have to do: Request the preprocessor to include the header file fstream. #include<fstream> Use declaration statements to declare the file streams we will use. Prepare each file for reaching and writing by using a function named open. Specify the name of the file stream in each input or output statement. Using Files Through the header file fstream, the C++ standard library defines two data types, ifstream and ostream. ifstream represents a stream of characters coming from an input file. ostream represents a stream of characters going to an output file. ALL the operators we have learned about cout and cin are valid with these data types. ifstream uses (>>) operator, get function, ignore function. ostream uses (<<) operator, endl, setw, setprecision, etc.. Declaring File Streams Just like we declared int, char, float, etc.. We declare file streams: int x; float y; ifstream myIn; ostream myOut; NOTE: Cannot read and write from the same file using the ifstream and the ostream. Opening Files Opening a file causes the computer’s operating system to perform certain actions that allow us to proceed with file I/O. Example of opening a file for reading and writing. myIn.open(“input.txt”); //Name of the file is irrelevant. myOut.open(“output.txt”); //Name of the file is irrelevant. Each of these are a function call to the open function. Both are different open functions, one is associated with the ifstream while the other the ostream. Open function Associates a stream variable used in your program with a physical file on disk. Open, with an input file, sets the file’s reading marker to the first piece of data in the file. Open, with an output file, checks whether the file already exists. If it does: If it doesn’t: Erases all the old contents of the file. Sets the writing marker to the beginning. Creates a new, empty file for you. Sets the writing marker to the beginning. NOTE: Want to open files before any kind of I/O attempts are made. Close Function Closing a file causes the operating system to perform certain wrapup activities on it and to break the connection between the stream variable and the file. Close function associated with both the ifstream and ostream. Do we always been to close files? Run-Time Input of File Names Open function associated with the ifstream data type requires an argument that specifies the name of the actual data file. Program will only work with this particular file, we don’t we want! We can to be able to run with any file. =) Command technique is to prompt the user for the name of the file to read. ifstream myIn; string fileName; cout << “Enter the input file name: “; cin >> fileName; myIn.open(fileName); // Compile-time error Run-Time of File Names Why do we get a compile-time error? Open function does not expect an argument of type string, but instead it expects a C string. We will talk more about C strings later, just KNOW that open doesn’t take string types. =) Lucky for us our string type does provide a function for us! c_str, value returning function. c_str function returns a C string that is equivalent to the one contained in the variable used to call it. fileName.c_str(); This function is to allow programmers to call library functions that expect C strings and not just string strings. =) Input Failure Things can go wrong whenever you input something to a program.\ Prompt user for int value, but we give it some char. cin will enter a fail state. Any further I/O operations using that stream are considered null operations. Computer doesn’t halt program execution or display an error. Invalid data is the most common reason for input failure.