Files Flashcards
ostream provides the «_space;operator, known as the
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.
An istream, short for “input stream,” is a class that supports input. Available via #include , istream provides the»_space; operator, known as the
extraction operator, to extract data from a data buffer and write the data into different types of variables.
An istream, short for “ ,” is a class that supports input.
input stream
An ostream, short for “ ,” is a class that supports output, available via #include and in namespace “std”.
output stream
A programmer can adjust the way that a program’s output appears, a task known as
output formatting
The main formatting approach uses manipulators. A manipulator is a
function that overloads the insertion operator «_space;or extraction operator»_space; to adjust the way output appears.
Manipulators are defined in the
iomanip and ios libraries in namespace std.
What are the Floating-point manipulators.
fixed
scientific
setprecision(p)
showpoint
myFloat = 85.4342;
cout «_space;fixed «_space;myFloat «_space;endl;
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
myFloat = 12.3423;
cout «_space;scientific «_space;setprecision(4) «_space;myFloat;
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).
The setw manipulator sets the number of characters for the
next output item only.
Floating-point manipulator
scientific
Use scientific notation.
From
cout «_space;scientific «_space;12.34;
Floating-point manipulator
setprecision(p)
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
Floating-point manipulator
showpoint
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;
cout «_space;setprecision(2) «_space;scientific;
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.
Text-alignment manipulators.
setw(n)
setfill(c)
left
right
Text-alignment manipulators.
setw(n)
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 «_space;setw(7) «_space;“Amy” «_space;endl;
cout «_space;setw(7) «_space;“George” «_space;endl;
Text-alignment manipulators.
setfill(c)
Sets the fill to character c.
From
// “**Amy”
cout «_space;setfill(‘*’) «_space;setw(7) «_space;“Amy”;
Text-alignment manipulators.
left
Changes to left alignment.
From
// “Amy “
cout «_space;left «_space;setw(7) «_space;“Amy”;
Text-alignment manipulators.
right
Changes back to right alignment.
From
// “ Amy”
cout «_space;right «_space;setw(7) «_space;“Amy”;
string str = “Amy”;
Which statement prints “…Amy”?
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 «_space;setw(6) «_space;setfill(‘.’) «_space;str;
string str = “Amy”;
Which statement prints “Amy”?
cout «_space; setfill(‘.’) «_space;str;
Since no width is specified, no places are filled with ‘.’, resulting in “Amy”.
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.
endl Inserts a newline character ‘\n’ into the output buffer
and informs the system to flush the buffer.
From
// Insert newline and flush
cout «_space;endl;
flush Informs the system to flush the buffer.
From
// Flush buffer
cout «_space;flush;
Sometimes a programmer wishes to read input data from a string rather than from the keyboard. A new input string stream variable of type
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.
Declare an istringstream variable named inSS that creates an input string stream using the string variable myStrLine.
istringstream inSS(myStrLine);
inSS.str(lineString); uses the str() function to initialize the stream’s buffer to string lineString.
// Copies to inSS’s string buffer
inSS.clear();
inSS.str(lineString);
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.
// Copies to inSS’s string buffer
inSS.clear();
inSS.str(lineString);
Which function is used to read an entire string from user input?
getline() is a standard library function for reading in a line of user input up until a newline character.