The setprecision manipulator cannot be used to format data written to a file.
The C language did not build the input/output facilities into the language. In other words, there is no keyword like typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;0 in typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;1 library). The ANSI C standard formalized these IO functions into Standard IO package ( typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;2). C++ continues this approach and formalizes IO in libraries such as typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;3 and typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;4. Show
Features
Stream IOC/C++ IO are based on streams, which are sequence of bytes flowing in and out of the programs (just like water and oil flowing through a pipe). In input operations, data bytes flow from an input source (such as keyboard, file, network or another program) into the program. In output operations, data bytes flow from the program to an output sink (such as console, file, network or another program). Streams acts as an intermediaries between the programs and the actual IO devices, in such the way that frees the programmers from handling the actual devices, so as to archive device independent IO operations. C++ provides both the formatted and unformatted IO functions. In formatted or high-level IO, bytes are grouped and converted to types such as typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;5, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;6, string or user-defined types. In unformatted or low-level IO, bytes are treated as raw bytes and unconverted. Formatted IO operations are supported via overloading the stream insertion ( typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;7) and stream extraction ( typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;8) operators, which presents a consistent public IO interface. To perform input and output, a C++ program:
C++ IO Headers, Templates and ClassesHeadersC++ IO is provided in headers typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9 (which included typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9 and typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9), typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9 (for file IO), and typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9 (for string IO). Furthermore, the header typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9 provided manipulators such as cout << value; cin >> variable;7, cout << value; cin >> variable;8 cout << value; cin >> variable;9 and cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;0 for formatting. Template ClassesIn order to support various character sets ( cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;1 and cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;2 in C++98/03; and cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;3, cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;4 introduced in C++11), the stream classes are written as template classes, which could be instantiated with an actual character type. Most of the template classes take two type parameters. For example, template > class basic_istream; template > class basic_ostream; where:
Template Instantiations and typedefAs mention, the 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 340 template classes can be instantiated with a character type, such as cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;1 and cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;2. C++ further provides 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 343 statements to name these classes: typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf; Specialization Classes for char typeWe shall focus on the specialization classes for cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;1 type:
Buffered IO[TODO] TheHeader and the Standard Stream Objects: cin, cout, cerr and clogThe typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9 header also included the these headers: typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9 and typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9. Hence, your program needs to include only the typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9 header for IO operations. The typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9 header declares these standard stream objects:
The Stream Insertion << and Stream Extraction >> OperatorsFormatted output is carried out on streams via the stream insertion typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;7 and stream extraction typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;8 operators. For example, cout << value; cin >> variable; Take note that The typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;7 and typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;8 operators are overloaded to handle fundamental types (such as typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;5 and typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;6), and classes (such as read 1). You can also overload these operators for your own user-defined types.The typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;09 and typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;10 return a reference to write 2 and write 8, and thus, support cascading operations. For example,cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ; The ostream ClassThe 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 372 class is a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 343 to 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 373. It contains two set of output functions: formatted output and unformatted output.
Formatting Output via the Overloaded Stream Insertion << OperatorThe 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 372 class overloads the stream insertion typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;7 operator for each of the C++ fundamental types ( cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;1, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;26, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;27, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;28, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;29, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;5, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;31, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;32, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;33, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;34 (C++11), typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;35 (C++11), typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;36, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;6 and typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;38. It converts a numeric value from its internal representation to the text form. The typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;7 operator returns a reference to the invoking 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 372 object. Hence, you can concatenate typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;7 operations, e.g., typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;42. The typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;7 operator is also overloaded for the following pointer types:
For example, Flushing the Output BufferYou can flush the output buffer via:
The istream classSimilar to the 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 372 class, the 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 370 class is a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 343 to 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 371. It also supports formatted input and unformatted input.
Formatting Input via the Overloaded Stream Extraction >> OperatorThe 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 370 class overloads the extraction typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;8 operator for each of the C++ fundamental types ( cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;1, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;26, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;27, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;28, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;29, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;5, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;31, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;32, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;33, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;34 (C++11), typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;35 (C++11), typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;36, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;6 and typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;38. It performs formatting by converting the input texts into the internal representation of the respective types. The typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;8 operator returns a reference to the invokind 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 370 object. Hence, you can concatenate typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;8 operations, e.g., typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;83. The typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;8 operator is also overloaded for the following pointer types:
[TODO] Read "C-string input". Flushing the Input Buffer - ignore()You can use the typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;88 to discard characters in the input buffer: Unformatted Input/Output Functionsput(), get() and getline()The 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 372's member function typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;21 can be used to put out a cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;1. typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;21 returns the invoking 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 372 reference, and thus, can be cascaded. For example, [TODO] Example read(), write() and gcount()Other istream functions - peek() and putback()States of streamThe steam superclass 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 345 maintains a data member to describe the states of the stream, which is a bitmask of the type typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;95. The flags are:
These flags are defined as public static members in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 345. They can be accessed directly via cout << value; cin >> variable;02 or via subclasses such as cout << value; cin >> variable;03, cout << value; cin >> variable;04. However, it is more convenience to use these public member functions of 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 346 class:
Formatting Input/Output via Manipulators inandC++ provides a set of manipulators to perform input and output formatting:
Default Output FormattingThe 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 372's typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;7 stream insertion operator is overloaded to convert a numeric value from its internal representation (e.g., 16-/32-bit typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;5, 64-bit typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;6) to the text form.
Field Width (setw), Fill Character (setfill) and Alignment (left|right|internal)The 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 345 superclass (included in typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9 header) maintains data members for field-width ( cout << value; cin >> variable;41) and formatting flags ( cout << value; cin >> variable;42); and provides member functions (such as cout << value; cin >> variable;43, cout << value; cin >> variable;44) for manipulating them. However, it is more convenience to use the so-called IO manipulators, which returns a reference to the invoking stream object and thus can be concatenated in typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;7 operator (e.g., cout << value; cin >> variable;46). They are:
The default field-width is 0, i.e., just enough space to display the value. C++ never truncates data, and will expand the field to display the entire value if the field-width is too small. The cout << value; cin >> variable;7 operation is non-sticky. That is, it is applicable only to the next IO operation, and reset back to 0 after the operation. The field-width property is applicable to both output and input operations. Except cout << value; cin >> variable;7, all the other IO manipulators are sticky, i.e., they take effect until a new value is set. Example: Alignment The cout << value; cin >> variable;55 alignment left-align the sign, but right-align the number, as illustrated. [TODO] Example of field-width for input operations You can also use 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 372's member function cout << value; cin >> variable;43 (e.g. cout << value; cin >> variable;58) to set the field width, but cout << value; cin >> variable;43 cannot be used with cout << value; cin >> variable;60 operator. Floating-point Format (fixed|scientific) and Precision (setprecision)The IO stream superclass 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 345 also maintains data member for the floating-point precision and display format; and provides member functions (such as cout << value; cin >> variable;62) for manipulating them. Again, it is more convenience to use IO manipulators, which can be concatenated in typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;7. They are:
Floating point number can be display in 3 formatting modes: cout << value; cin >> variable;68 cout << value; cin >> variable;69. The precision is interpreted differently in default and non-default modes (due to legacy).
For examples, You can also use 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 372's member function cout << value; cin >> variable;81 (e.g. cout << value; cin >> variable;82) to set the floating-point precision, but cout << value; cin >> variable;62 cannot be used with cout << value; cin >> variable;60 operator. Integral Number Base (dec|oct|hex, setbase)C++ support number bases (radixes) of decimal, hexadecimal and octal. You can use the following manipulators (defined in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 345 class, included in typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9 header) to manipulate the integral number base:
These manipulators are sticky. For examples, bool values (boolalpha|noboolalpha)
Other manipulators
Notes
The C++ string class Input/Output[TODO] File Input/Output (Header)C++ handles file IO similar to standard IO. In header typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9, the class typedef basic_istringstream istringstream; typedef basic_ostringstream ostringstream;6 is a subclass of 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 372; typedef basic_istringstream istringstream; typedef basic_ostringstream ostringstream;5 is a subclass of 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 370; and typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;4 is a subclass of typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;3 for bi-directional IO. You need to include both typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9 and typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9 headers in your program for file IO. To write to a file, you construct a cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;24 object connecting to the output file, and use the 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 372 functions such as stream insertion typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;7, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;21 and typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;22. Similarly, to read from an input file, construct an typedef basic_istringstream istringstream; typedef basic_ostringstream ostringstream;5 object connecting to the input file, and use the 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 370 functions such as stream extraction typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;8, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;61, typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;98 and typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;63. File IO requires an additional step to connect the file to the stream (i.e., file open) and disconnect from the stream (i.e., file close). File OutputThe steps are:
By default, opening an output file creates a new file if the filename does not exist; or truncates it (clear its content) and starts writing as an empty file. open(), close() and is_open()File ModesFile modes are defined as static public member in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 345 superclass. They can be referenced from 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 345 or its subclasses - we typically use subclass 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 346. The available file mode flags are:
You can set multiple flags via bit-or ( cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;49) operator, e.g., cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;50 to append output at the end of the file. For output, the default is cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;51. For input, the default is cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;52. The steps are:
By default, opening an input file .... Example on Simple File IO1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 Program Notes:
Binary file, read() and write()We need to use typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;63 and typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;22 member functions for binary file (file mode of cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;68), which read/write raw bytes without interpreting the bytes. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 Random Access FileRandom access file is associated with a file pointer, which can be moved directly to any location in the file. Random access is crucial in certain applications such as databases and indexes. You can position the input pointer via cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;69 and output pointer via cout << value1 << value2 << .... ; cin >> variable1 << variable2 << .... ;70. Each of them has two versions: absolute and relative positioning. Random access file is typically process as binary file, in both input and output modes. [TODO] Example String StreamsC++ provides a typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;9 header, which uses the same public interface to support IO between a program and read 1 object (buffer).The string streams is based on typedef basic_istringstream istringstream; typedef basic_ostringstream ostringstream;9 (subclass of 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 372), typedef basic_istringstream istringstream; typedef basic_ostringstream ostringstream;8 (subclass of 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 370) and bi-directional read 0 (subclass of typedef basic_ios ios; typedef basic_ios wios; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ostream ostream; typedef basic_ostream wostream; typedef basic_iostream iostream; typedef basic_iostream wiostream; typedef basic_streambuf streambuf; typedef basic_streambuf wstreambuf;3). Which class can be used to create files that can be written to but not read from C++?Either ofstream or fstream object may be used to open a file for writing. And ifstream object is used to open a file for reading purpose only.
What data type or class is used to open existing files and read data from them into memory?In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream. fstream: This Stream class can be used for both read and write from/to files.
Which statement opens a file for appending quizlet?"a" opens a file for appending data to the end of the file. "rb" opens a file for reading binary data. "wb" opens a file for writing binary data.
Which of the following positions the file pointer for a file that has been opened for reading and writing?Which of the following positions the file pointer for a file that has been opened for reading and writing? Use the seekp(arg) fstream member function with the number of bytes to the record in question (counting the first byte as 0) as argument to position the file pointer.
|