Chapter 12 Review Questions
Should file stream objects be passed to functions by value or by reference? Why?
passed by reference
T/F: If a file is opened in the definition of the file stream object, no mode flags may be specified.
False
T/F: ifstream objects, by default, create a file if it doesn't exist when opened.
False, if the file doesn't exist, the open function fails
T/F: fstream objects are only capable of performing file output operations.
False, they can do input AND output
T/F: A file may be opened in the definition of the file stream object.
True
T/F: A file stream object's fail member function may be used to determine if the file was successfully opened.
True
T/F: Different operating systems have different rules for naming files.
True
T/F: Several file access flags may be joined by using the | operator.
True
T/F: The >> operator expects data to be delimited by whitespace characters.
True
T/F: The getline member function can be used to read text that contains whitespaces.
True
T/F: The same output formatting techniques used with cout may also be used with file stream objects.
True
T/F: ofstream objects, by default, delete the contents of a file if it already exists when opened.
True, if the file already exists, the contents are deleted
How do you read the contents of a text file that contains whitespace characters as part of its data?
Use the getline function to read a line of data
Assume that the file "data.txt" already exists, and the following statement executes. What happens to the file? fstream file("data.txt", ios::out);
Data will be written to the file. By default, the file's contents will be deleted if it already exists, which is the case in this question.
Under what circumstances is a file stream object's "ios::badbit" bit set? What member function reports the state of this bit?
Set when an invalid operation has been attempted. bad() --> returns true if the badbit flag is set, otherwise returns false
Under what circumstances is a file stream object's "ios::hardfail" bit set? What member function reports the state of this bit?
Set when an unrecoverable error has occurred. fail() --> returns true if hardfail flags are set, otherwise returns false
Under what circumstances is a file stream object's "ios::eofbit" bit set? What member function reports the state of this bit?
Set when the end of an input stream is encountered. eof() --> returns true if the eofbit flag is set, otherwise returns false
If a file fails to open, the file stream object will be set to ___.
Zero
The same formatting techniques used with ___ may also be used when writing data to a file.
cout
How do you combine multiple file access flags when opening a file?
dataFile.open("info.txt", ios::in, ios::out);
The ___ file stream data type is for output files, input files, or files that perform both input and output.
fstream
What capability does the fstream data type provide that the ifstream and ofstream data types do not?
fstream allows for both reading and writing; if stream is ONLY reading and ofstream is ONLY writing
The ___ function reads a line of text from a file.
getline
Which file access flag do you use to open a file when you want all output written to the end of the file's existing contents?
ios::app