cin
cin >> str;
char c = std::cin.get(); 读入一个字符;
std::istream::get
single character (1)
int get();
istream& get (char& c);
c-string (2)
istream& get (char* s, streamsize n);
istream& get (char* s, streamsize n, char delim);
stream buffer (3)
istream& get (streambuf& sb);
istream& get (streambuf& sb, char delim);
cout
cout << str;
cerr
ifstream
ofstream
fstream不会创建文件,只打开文件并和文件关联起来、关闭文件时和文件解除绑定
fstream
方式1:构造时关联文件;
方式2:构造时为空,open时关联文件;
方式3:构造时为空,读取rdbuf指针,通过此指针open关联文件;(可以看出rdbuf永远不为空,返回filebuf 类型的指针)
常用用法:
1.
std::ostringstream oss;
oss << "One hundred and one: " << 101;
std::string s = oss.str();
2.
用friend <<输出
class Function
{
friend ostream& operator <<(ostream & s_, const Function & desc_);
}
3.
ifstream inputFile("interestingData.txt"); string fileData((istreambuf_iterator<char>(inputFile)), istreambuf_iterator<char>());
成员函数判断状态
iostate value (member constants) | indicates | functions to check state flags | ||||
---|---|---|---|---|---|---|
good() | eof() | fail() | bad() | rdstate() | ||
goodbit | No errors (zero value iostate) | true | false | false | false | goodbit |
eofbit | End-of-File reached on input operation | false | true | false | false | eofbit |
failbit | Logical error on i/o operation | false | false | true | false | failbit |
badbit | Read/writing error on i/o operation | false | false | true | true | badbit |
<iomanip>
-
/*undefined*/ setw (int n); 设置宽度,用于输入和输出
std::cout << std::setw(10); - /*unspecified*/ setfill (char_type c); 设置填充字符(用于输出ostream)
- /*unspecified*/ setprecision (int n); 设置浮点数精度,用于输入和输出
- /*unspecified*/ setbase (int base); 设置几进制的数,用于输入和输出
- 例子:
std::ios_base::setf
- fmtflags setf (fmtflags fmtfl);
- fmtflags setf (fmtflags fmtfl, fmtflags mask);
field member constant effect when set independent flags boolalpha read/write bool elements as alphabetic strings ( true
andfalse
).showbase write integral values preceded by their corresponding numeric base prefix. showpoint write floating-point values including always the decimal point. showpos write non-negative numerical values preceded by a plus sign (+). skipws skip leading whitespaces on certain input operations. unitbuf flush output after each inserting operation. uppercase write uppercase letters replacing lowercase letters in certain insertion operations. numerical base
(basefield)dec read/write integral values using decimal base format. hex read/write integral values using hexadecimal base format. oct read/write integral values using octal base format. float format
(floatfield)fixed write floating point values in fixed-point notation. scientific write floating-point values in scientific notation. adjustment
(adjustfield)internal the output is padded to the field width by inserting fill characters at a specified internal point. left the output is padded to the field width appending fill characters at the end. right the output is padded to the field width by inserting fill characters at the beginning.
iostate value (member constants) | indicates | functions to check state flags | ||||
---|---|---|---|---|---|---|
good() | eof() | fail() | bad() | rdstate() | ||
goodbit | No errors (zero value iostate) | true | false | false | false | goodbit |
eofbit | End-of-File reached on input operation | false | true | false | false | eofbit |
failbit | Logical error on i/o operation | false | false | true | false | failbit |
badbit | Read/writing error on i/o operation | false | false | true | true | badbit |