cin, cout, ifstream, ofstream, istringstream, ostringstream, <iomanip>
2013-04-02 16:25
796 查看
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 |
/*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 (trueand false). |
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 |
相关文章推荐
- error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)
- error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)
- error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)
- implicit instantiation of undefined template 'std::basic_stringstream<char,std::char_trats<char>,std
- <C++>istringstream, ostringstream, stringstream的用法
- error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)
- <C/C++> IO流 cout/fstream的使用
- #include<sstream> string转化为double
- <sstream>—istringstream、ostringstream、stringstream类
- error LNK2005: "public: void __thiscall std::basic_ofstream<char,struct std::char_traits<char> >::`v
- wsdl2java jaxbelement<string> instead of string
- C++库常用函数一览(<string><cctype><algorithm><cmath><cstdlib><iomanip><numeric>)
- <c:if test="${fn:contains(name, searchString)}">
- error LNK2005: "public: void __thiscall std::basic_ofstream<char,struct std::char_traits<char>
- error C2514: “std::basic_stringstream<_Elem,_Traits,_Alloc>”: 类没有构造函数
- error C2027: 使用了未定义类型 std::basic_ifstream<_Elem,_Traits>
- C++库常用函数一览(<string><cctype><algorithm><cmath><cstdlib><iomanip><numeric>)
- <LeetCode OJ> 345. Reverse Vowels of a String
- error C2079: “out”使用未定义的 class“std::basic_ofstream<_Elem,_Traits>”
- c++ 头文件<cstring>(或者string.h)中的常见函数的实现!