cin, cout, ifstream, ofstream, istringstream, ostringstream, <iomanip>

本文详细介绍了C++中标准IO流的操作方法,包括输入输出流的使用、错误状态的检查及格式化输出等。同时提供了多种实例演示如何进行字符串、文件的读写以及如何设置输出格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



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()
goodbitNo errors (zero value iostate) truefalsefalsefalsegoodbit
eofbitEnd-of-File reached on input operationfalse truefalsefalseeofbit
failbitLogical error on i/o operationfalsefalse truefalsefailbit
badbitRead/writing error on i/o operationfalsefalse true truebadbit




<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)
indicates functions to check state flags
good()eof()fail()bad()rdstate()
goodbitNo errors (zero value iostate) truefalsefalsefalsegoodbit
eofbitEnd-of-File reached on input operationfalse truefalsefalseeofbit
failbitLogical error on i/o operationfalsefalse truefalsefailbit
badbitRead/writing error on i/o operationfalsefalse true truebadbit

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值