Files Flashcards

1
Q

ostream provides the &laquo_space;operator, known as the

A

insertion operator, for converting different types of data into a sequence of characters. That sequence is normally placed into a buffer, and the system then outputs the buffer at various times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

An istream, short for “input stream,” is a class that supports input. Available via #include , istream provides the&raquo_space; operator, known as the

A

extraction operator, to extract data from a data buffer and write the data into different types of variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

An istream, short for “ ,” is a class that supports input.

A

input stream

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

An ostream, short for “ ,” is a class that supports output, available via #include and in namespace “std”.

A

output stream

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A programmer can adjust the way that a program’s output appears, a task known as

A

output formatting

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The main formatting approach uses manipulators. A manipulator is a

A

function that overloads the insertion operator &laquo_space;or extraction operator&raquo_space; to adjust the way output appears.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Manipulators are defined in the

A

iomanip and ios libraries in namespace std.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the Floating-point manipulators.

A

fixed
scientific
setprecision(p)
showpoint

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

myFloat = 85.4342;

cout &laquo_space;fixed &laquo_space;myFloat &laquo_space;endl;

A

85.434
The fixed manipulator sets to fixed-point notation. For fixed, setprecision sets the max number of digits in fraction only (after the decimal point).
Is a Floating Point Manipulator. Use fixed-point notation.
From

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

myFloat = 12.3423;

cout &laquo_space;scientific &laquo_space;setprecision(4) &laquo_space;myFloat;

A

Expected
1.2342e+01
✖ The scientific manipulator sets to scientific notation. For scientific, setprecision sets the max number of digits in fraction only (after the decimal point).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The setw manipulator sets the number of characters for the

A

next output item only.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Floating-point manipulator

scientific

A

Use scientific notation.
From
cout &laquo_space;scientific &laquo_space;12.34;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Floating-point manipulator

setprecision(p)

A

If stream has not been manipulated to fixed or scientific:
Sets max number of digits in number

If stream has been manipulated to fixed or scientific:
Sets max number of digits in fraction only (after the decimal point).
From

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Floating-point manipulator

showpoint

A
Even if fraction is 0, show decimal point and trailing 0s.
Opposite is noshowpoint.
From 
// 99
cout << setprecision(3) << 99.0;
// 99.0
cout << setprecision(3) << showpoint << 99.0;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

cout &laquo_space;setprecision(2) &laquo_space;scientific;

A

Turning on scientific notation causes the number to output with an e and a power, and setting the precision to 2 outputs the number with 2 decimal places. The ordering of the scientific and setprecision manipulators does not matter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Text-alignment manipulators.

A

setw(n)
setfill(c)
left
right

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Text-alignment manipulators.

setw(n)

A

Sets the number of characters for the next output item only
(does not persist, in contrast to other manipulators).
By default, the item will be right-aligned, and filled with spaces.
From
// “ Amy”
// “ George”
cout &laquo_space;setw(7) &laquo_space;“Amy” &laquo_space;endl;
cout &laquo_space;setw(7) &laquo_space;“George” &laquo_space;endl;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Text-alignment manipulators.

setfill(c)

A

Sets the fill to character c.
From
// “**Amy”
cout &laquo_space;setfill(‘*’) &laquo_space;setw(7) &laquo_space;“Amy”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Text-alignment manipulators.

left

A

Changes to left alignment.
From
// “Amy “
cout &laquo_space;left &laquo_space;setw(7) &laquo_space;“Amy”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Text-alignment manipulators.

right

A

Changes back to right alignment.
From
// “ Amy”
cout &laquo_space;right &laquo_space;setw(7) &laquo_space;“Amy”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

string str = “Amy”;

Which statement prints “…Amy”?

A

setw and setfill apply to the next output, so “Amy” is output with a width of 6 characters and filled with “…” on the left, leading to “…Amy”.

cout &laquo_space;setw(6) &laquo_space;setfill(‘.’) &laquo_space;str;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

string str = “Amy”;

Which statement prints “Amy”?

A

cout &laquo_space; setfill(‘.’) &laquo_space;str;

Since no width is specified, no places are filled with ‘.’, resulting in “Amy”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Buffer manipulators.

To preserve resources, the system may wait until the output buffer is full, or at least has a certain number of characters, before moving the characters to the output device. Or, with fewer characters in the buffer, the system may wait until the resources are not busy. Sometimes a programmer does not want the system to wait. Ex: In a very processor-intensive program, waiting could cause delayed and/or jittery output.

Two manipulators exist to send all buffer contents to the output device without waiting: endl and flush.

A

endl Inserts a newline character ‘\n’ into the output buffer
and informs the system to flush the buffer.
From
// Insert newline and flush
cout &laquo_space;endl;

flush Informs the system to flush the buffer.
From
// Flush buffer
cout &laquo_space;flush;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Sometimes a programmer wishes to read input data from a string rather than from the keyboard. A new input string stream variable of type

A

istringstream can be created that reads input from an associated string instead of the keyboard (standard input). istringstream is derived from istream. An istringstream can be used just like the cin stream as illustrated in the program below.

The program uses #include for access to the string stream class, which is in namespace std.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Declare an istringstream variable named inSS that creates an input string stream using the string variable myStrLine.

A

istringstream inSS(myStrLine);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

inSS.str(lineString); uses the str() function to initialize the stream’s buffer to string lineString.

A

// Copies to inSS’s string buffer
inSS.clear();
inSS.str(lineString);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

The statement inSS.clear(); is necessary to reset the state of the stream so that subsequent extractions start from the beginning of the input strings.

A

// Copies to inSS’s string buffer
inSS.clear();
inSS.str(lineString);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Which function is used to read an entire string from user input?

A

getline() is a standard library function for reading in a line of user input up until a newline character.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Which function resets the string stream’s state?

A

clear() is an iostream function that resets the stream’s state, allowing new strings copied into the stream’s buffer to be read from the beginning

30
Q

Which function copies the input string into the string stream?

A

Which function copies the input string into the string stream?

31
Q

A programmer will not always know how much data exists in a user input string, so using multiple individual extractions (Ex: inSS&raquo_space; data1; inSS&raquo_space; data2;) is not useful for reading all of the input data. Input streams have a Boolean function called

A

eof() or end of file that returns true or false depending on whether or not the end of the stream has been reached.

32
Q

Which function copies the input string into the string stream?

A

str() is a string stream function that copies a string into the string stream’s input buffer.

33
Q

If in the loop condition inSS&raquo_space; data, inSS’s eof() function returns true, what does the condition inSS&raquo_space; data resolve to?

A

When the end of the stream is reached, the program should not attempt to read more data, so inSS&raquo_space; data resolves to false to end the loop.

34
Q

What function checks if the string stream is in an error state?

A

The good() function returns true when a stream is not in an error state and false otherwise. Ex: If a program attempts to read a character in a stream’s buffer into an integer variable, the stream is placed into an error state.

35
Q

An output string stream variable of type ostringstream can

A

insert characters into a string buffer instead of the screen (standard output). ostringstream is derived from ostream.
A program can insert characters into an ostringstream buffer using

36
Q

The ostringstream member function str() returns the contents of

A

an ostringstream buffer as a string.

37
Q

ostringstream infoOSS; declares

A

a new output string stream variable. The stream’s buffer is initially empty

38
Q

// Write user input to string stream
infoOSS &laquo_space;lastName &laquo_space;”, “ &laquo_space;firstName;
infoOSS &laquo_space;” “ &laquo_space;userAge;

A

After reading input from the keyboard, the program inserts data into infoOSS’s buffer using

39
Q

After reading input from the keyboard, the program inserts data into infoOSS’s buffer using

A

he string “ (minor)” is also inserted into the string stream buffer because the user entered an age < 21

40
Q

// Extract string stream buffer as a single string

A

infoStr = infoOSS.str();

41
Q

Write a statement that copies the contents of an output string stream called outSS to an existing string variable called myStr.

A

myStr = outSS.str();

The ostringstream’s str() member function returns the output string stream’s buffer as a single string.

42
Q

Which statement can be added to ProduceSavingsTable() to produce a line of dashes across the table?

A

outSS &laquo_space;setfill(‘-‘) &laquo_space;setw(15) &laquo_space;””;

The setfill manipulator sets the fill character used by setw to a dash, so 15 dashes are output.

43
Q

what function can request a new input stream that comes from a file, rather than the predefined input stream cin that comes from the standard input (keyboard)

A

The inFS.open(str) function has a string parameter str that specifies the name of the file to open.

44
Q

what function can request a new input stream that comes from a file, rather than the predefined input stream cin that comes from the standard input (keyboard)

A

The inFS.open(str) function has a string parameter str that specifies the name of the file to open.

A program can also use a user-entered string as the filename, such as using cin&raquo_space; filename;.

45
Q

ifstream inFS; // Input file stream

A

A new stream variable, ifstream inFS, is declared. ifstream is short for input file stream, and is derived from istream.

46
Q

File input.

Which function checks that a file has been opened?

A

inFS.open(“numFile.txt”);

inFS.open(“numFile.txt”); opens the file for reading and associates the file with the inFS stream

47
Q

A program can read varying amounts of data in a file by using a loop that reads until the end of the file has been reached.

A

The eof() function returns true if the previous stream operation reached the end of the file.

48
Q

Errors may be encountered while attempting to read from a file, like the inability to read the file, reading corrupt data, etc. So, a program should check that each read was successful before using the variable to which the read data was assigned. The

A

fail() function returns true if the previous stream operation had an error.

49
Q

Which returns whether a logical error occurred with file input stream inFS?

A
inFS.fail()
inFS is a class with the member function fail().
50
Q

An ofstream, short for “output file stream”, is a class that supports

A

writing to a file. The ofstream class inherits from ostream.

51
Q

An ofstream, short for “output file stream”, is a class that supports writing to a file. The ofstream class inherits from ostream.

After declaring a variable of type ofstream, a file is opened using the

A

ofstream’s open() function.

52
Q

The ofstream’s is_open() function is commonly called to check if the file

A

opened successfully. If so, data can be written to the file using the &laquo_space;operator.

When all desired data is written, the ofstream’s close() function is called to close the file.

53
Q

The statement cin&raquo_space; inString; may buffer the full “one two” line typed by the user, but only copies content before the

A

first space character into inString. Thus the string “one” is read into inString, and then is added to the waiting line.

54
Q

The WaitingLine class uses the insertion operator to add a string to the

A

back of the line and the extraction operator to remove a string from the front of the line.

55
Q

By default, a programmer-defined C++ class does not work with cin and cout. Statements like cin&raquo_space; line1 or cout &laquo_space;line1, where line1 is a WaitingLine object, cause a compiler error. But the functionality of cin and cout can be extended by implementing certain friend functions in the C++ class.

A

friend ostream& operator<>(istream& in, WaitingLine& line) {

Since friend istream& operator» is implemented in the WaitingLine class, cin&raquo_space; line1 can be used to get input from the user and add to the line.

56
Q

reviewsFS

A

reviewsFS is used like cout with &laquo_space;to receive the file output

57
Q

close()

A

Closing the file stream is good practice when the program no longer needs to output to the file stream.

58
Q

open(“reviews.txt”)

A

reviews.txt must be opened before output can be sent to reviewsFS.
3)

59
Q

ofstream

A

SaveReviews() must output the reviews to reviews.txt, and an ofstream is used for outputting to a file.

60
Q

When used by itself, the ios::app flag causes the file’s existing contents to be deleted if the file already exists.

A

False

61
Q
An alternative to using the open member function is to use the file stream object declaration itself to open the file. For example:
fstream DataFile("names.dat", ios::in | ios::out);
A

True

62
Q

The setprecision manipulator cannot be used to format data written to a file.

A

False

63
Q

Which of the following statements opens a file named info.txt for both input and output?

A

dataFile.open(“info.txt”, ios::in | ios::out);

64
Q

What is TRUE about the following statement?

out.open(“values.dat”, ios::app);

A

If the file already exists, its contents are preserved and all output is written to the end of the file.

65
Q

ofstream, ifstream, and fstream are

A

data types

66
Q

Which statement opens a file in such a way that information will only be written to the end of the file?

A

dataFile.open(“info.dat”, ios::out | ios::app);

67
Q

Which of the following access flags, when used by itself, causes a file’s contents to be deleted if the file already exists?

A

ios::out

68
Q

Closing a file causes any unsaved information still held in the file buffer to be

A

saved to the file

69
Q

The end-of-file marker is automatically written

A

when a file is closed

70
Q

Which of the following is the member function that writes a single character to a file?

A

put