您的位置:首页 > 其它

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)
indicatesfunctions to check state flags
good()eof()fail()bad()rdstate()
goodbitNo errors (zero value iostate)
true
false
false
false
goodbit
eofbitEnd-of-File reached on input operation
false
true
false
false
eofbit
failbitLogical error on i/o operation
false
false
true
false
failbit
badbitRead/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);

fieldmember constanteffect when set
independent flagsboolalpharead/write bool elements as alphabetic strings (
true
and
false
).
showbasewrite integral values preceded by their corresponding numeric base prefix.
showpointwrite floating-point values including always the decimal point.
showposwrite non-negative numerical values preceded by a plus sign (+).
skipwsskip leading whitespaces on certain input operations.
unitbufflush output after each inserting operation.
uppercasewrite uppercase letters replacing lowercase letters in certain insertion operations.
numerical base

(basefield)
decread/write integral values using decimal base format.
hexread/write integral values using hexadecimal base format.
octread/write integral values using octal base format.
float format

(floatfield)
fixedwrite floating point values in fixed-point notation.
scientificwrite floating-point values in scientific notation.
adjustment

(adjustfield)
internalthe output is padded to the field width by inserting fill characters at a specified internal point.
leftthe output is padded to the field width appending fill characters at the end.
rightthe output is padded to the field width by inserting fill characters at the beginning.
iostate value

(member constants)
indicatesfunctions to check state flags
good()eof()fail()bad()rdstate()
goodbitNo errors (zero value iostate)
true
false
false
false
goodbit
eofbitEnd-of-File reached on input operation
false
true
false
false
eofbit
failbitLogical error on i/o operation
false
false
true
false
failbit
badbitRead/writing error on i/o operation
false
false
true
true
badbit

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐