C++学习笔记——输入输出

C++ I/O

输入输出

输入输出操作

  • 格式化/解析:格式化(将数转为字符串)解析(将字符串转为2进制数)
  • 缓存:将字符序列存入缓存
  • 编码转换:转为Unicode utf8
  • 传输:将结果传到终端

格式化与非格式化

非格式化I/O

输入函数:get read geiline gcount

输出函数:put write

格式化I/O

用移位操作符进行输入>> 输出<<

//格式化控制
char a = 'a';
cout.width(10);
cout.fill('.');
std::cout<<a<<std::endl; //0输出:.........a


#include <iomanin>
char a[5];
std::cin>>std::setw(5)>>x;  //键盘输入abcde
std::cout<<x<<std::endl;    //输出abcd,最后一位为'/0'结束位

文件流

#include <fstream>
//using ifstream = basic_ifstream<char>;
//using ofstream = basic_ofstream<char>;

std::ofstream outfile("my_file");
outfile<<"hello";  //创建一个文件my_file 写入hello

std::ifstream  infile("my_file");
std::string x;
infile>>x;  //读文件my_file 将内容读入到字符串x中
std::cout<<x<<std::endl; //输出:hello

std::infile.isopen();  //判断文件是否正常打开,ifstream用有效文件名初始化视为打开

//或用以下方式打开
std::ifstream infile;
ifstream.open("my_file");  //打开
infile.close();  //关闭

打开模式

函数参数(std::ios_base:😃作用
in打开以供读取 ifstream缺省实参 (fstream缺省实参in
out打开以供写入 ofstream2缺省实参
ate初始位置位于文件结尾,但还是会截断
app附加文件,向文件结尾写入
trunc截断文件 删除文件内容
binary二进制模式 禁止系统特定转换,如windows中\n 为 \r\n

合并打开方式组合

ifstream:
in:只读打开
in | ate :只读打开,初始位于结尾
in | binary :禁止系统转换

ofstream:
out | app :打开或建立文件,在文件结尾开始写入
out | trunc :截断文件,删除文件内容在写入

fstream:
in | out :打开文件支持读写
in | out | app :打开文件支持读写,在结尾写入
in | out | trunc :打开文件支持读写,截断文件

内存流

#include <sstream>
//using stringstream = basic_string_stream<char>;

std::ostringstream = obj;
obj<<1234;  //系统会格式化转为"1234"
std::string res = obj.str(); //"1234" //内存流与文件流不同,打开不会截断文件,默认初始位置在开头

std::istringstream obj2(res);
int x;
obj2>>x;  //x:1234


用内存流拼接字符串
std::ostringstream ostr;
ostr<<"hello";
ostr<<"world";
std::string y = ostr.str();
流的定位和同步

流的定位

获取当前流的位置 返回pos_type pos
tellg() //g=get,istringstream用 
tellp() //p=put,ostringstream用

std::ostringstream s;
s<<'h';
std::cout<<s.teep();  //1

std::string str = "hello, world";
std::istringstream in(str);
std::string world;
in>>word;  
std::cout<<word;  //hello,
std::cout<<in.tellg();  //6
stringstream输入内容到字符串时,从第一个不为停止字符的字符开始直到第一个停止字符停止,默认停止字符为空格

seekg() //input用 设置输入输出流位置
seekp() //output用 设置输入输出流位置
绝对位置seekg(pos_type pos)
相对位置seekg(off_type off, std::ios_base::seekdir dir) beg end cur

std::ostringstream os("hello, world");
os.seekp(7);
os<<'w';
os.seekp(0,std::ios_base::end);
os<<'!';

std::string str = "hello, world";
std::istringstream in(str);
in>>word1;  //"hello,"
in.seekg(0);
in>>word2;  //"hello,"

流的同步

flush() 输出流同步,刷新缓冲区
syn() 输入流同步
std::cout<<"hello"<<std::endl;  //刷新缓冲区
std::cout<<"hello"<<std::flush;  //刷新缓冲区
std::cout.flush();  //刷新缓冲区
std::cout<<std::unitbuf<<"hello";  //刷新缓冲区

//流的绑定
std::ofstream os("test.txt");
std::ifstream is("test.txt");
std::string value("0");
os<<"hello";
is>>value;  //此时未刷新缓冲区value不变

is.clear()
is.tie(&os);
is>>value;  //绑定后输入输出前刷新缓冲区故value为"hello"

流的状态

位掩码类型
std::ios_base::iostate
failbit:输入输出操作失败(格式化/提取错误)
badbit:不可恢复错误
eofbit:关联的输出序列已达结尾(end of file)
goodbit:无错误

int x;
std::cin>>x;  //failbit
输入时 ctrl+D 强行终止输入或流的结尾  //eofbit

检测流的状态
std::cout<<std::cin.good();
           std::cin.fail();
           std::cin.bad();
           std::cin.eof();
           static_cast<bool>(std::cin);

std::cin>>x 返回cin
eofbitfailbitbadbitgood()fail()bad()eof()bool
FFFTFFFT
FFTFTTFF
FTFFTFFF
FTTFTTFF
TFFFFFTT
TFTFTTTF
TTFFTFTF
TTTFTTTF
  • 转换为bool值时不会考虑eof
  • 设置流状态 setstate
  • 清空流状态
std::cin.clear();
std::cin.sync(); //必须刷新缓冲区,否则上次错误还在缓冲区内,再次输入错误
  • noskipws使用时必须注意
char a;
std::cin>>std::noskipws>>a;
//此时可以把空格等字符存入a中
但要注意字符串,字符串默认是从第一个不是空白符到第一个空白符,读取时加了noskipws后第一个读入的就是空白符,被认为是字符串的结束,导致字符串读取不到内容
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值