当前位置: 首页 > news >正文

C++学习(7)(输入输出)

输入和输出

所有业务的本质都是输入和输出

输出有相应的格式

//控制台输出ostream #include <iostream> using namespace std; int main() { //cout标准输出ostream cout << "test cout" << endl; cout << 100 << endl;//默认十进制输出 cout << oct << 100 << endl;//八进制输出 cout << hex << 100 << endl;//十六进制输出 cout << 100 << endl;//每次使用完要记得恢复,编译器更改不是一次性的 cout << dec << 100 << endl; cout << boolalpha;//bool类型输出 cout << true << ": " << false << endl; std::cout << "Hello World!\n"; } Cout无格式输出 //cout无格式输入 cout.put('a').put('b'); cout << endl; cout.write("123", 3); string str1 = "testing"; cout.write(str1.c_str(), str1.size()); cout << flush;//刷新缓冲区 //cout.put和cout.weite的区别 //put是输出一个字符 //write是输出字符串 标准输出和错误输出 重定向 //cerr标准错误输出 无缓冲 stderr 2 cerr << "test cerr 01\n"; cerr << "TEST CERR 02\n"; std::cout << "Hello World!\n"; 控制台不同的文件编号 Istream #include <iostream> #include <string> using namespace std; int main() { //错误检查 while (1) { int x{ 0 }; cin >> x; if (cin.fail()) { string rubish; cin.clear();//恢复状态为正常 getline(cin, rubish);//清除垃圾 cout << "error" << endl; continue; } cout << "x :" << x << endl; } //单个字符输入get char ch = cin.get(); cout << ch << endl;//可以输入很多但是只读一个 string cmd; while (1) { char ch = cin.get(); cmd += ch;//虽然一次只读一个,也是按下回车健才执行 if (ch == '\n') { cout << "cmd: " << cmd << endl; cmd = ""; } } //单行输入getline char buf[100]{ 0 }; cin.getline(buf, sizeof(buf) - 1); //要最后给\0有一个空 cout << buf << endl; //如果想要多行输入,可以一个死循环,加一个结束符 while(1) { char buf[100]{ 0 }; cin.getline(buf, sizeof(buf) - 1); cout << buf << endl; if (strstr(buf, "over")) { break; } } string line; getline(cin, line); cout << line << endl; std::cout << "Hello World!\n"; }

Stringstream = istream+ostream

#include <iostream> #include <sstream> #include <string> using namespace std; int main() { //基础用法 { //简单理解成cout的自由拼接 stringstream ss; ss << "test : " << 100; ss << true; ss << boolalpha; ss << hex; ss << "\n" << false << 100 << endl; cout << ss.str() << endl; ss.str("");//传入空字符串代表清空 } //特殊用法 { //stringstream格式输入 string data = "teat1 teas2 "; stringstream ss(data); string tmp; cout << ss.str() << endl; ss >> tmp; cout << tmp << endl;//传入到空格为止 } { //stringstream单行读取 string data = "test1 test2 \n test3 test4"; string line; stringstream ss(data); getline(ss, line); cout << "line: " << line<<endl; } { //多行读取 string data = "test1 test2 \n test3 test4"; string line; stringstream ss(data); while (1) { getline(ss, line); cout << "line: " << line << endl; if (ss.eof())//结尾处理 break; } } std::cout << "Hello World!\n"; }
http://www.jsqmd.com/news/409752/

相关文章:

  • 开局即冲刺:FPC行业新春开工观察与2026年四大核心趋势
  • 构建AI Agent驱动的智能营销系统
  • 建立并添加dbc文件
  • 提示工程架构师工具推荐:5款助力AI提示系统可持续性设计的AI辅助工具
  • Kimi正进行新一轮7亿美元融资:阿里腾讯加持 估值超百亿美元
  • 上班这件事,接受它的不完美,但认真对待它
  • 美妆店化妆品商城系统 美容院预约系统 uniapp微信小程序设计与实现
  • 团队为什么越到关键节点越容易内讧?
  • 微信小程序的uniapp闲置物品处置捐赠平台的设计与实现
  • Spring boot名城小区物业管理系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
  • 微信小程序的uniapp餐厅预约系统设计与实现
  • 微信的校园跑腿系统 小程序的设计与实现
  • 微信小程序的uniapp植物识别与植物养护经验交流平台
  • 动态加载JS文件控制台不显示的问题与解决
  • 微信小程序的uniapp选修课管理系统的设计与实现
  • 微信小程序的uniapp二手书交易私信聊天系统的设计与实现
  • 1343: PIPI的字符串问题Ⅰ
  • 微信小程序的uniapp校园活动报名管理系统设计与实现
  • 微信小程序的DIY电脑推荐与交流平台
  • 输入输出上的问题
  • 基于Java+SpringBoot+SSM自行车仓库管理系统(源码+LW+调试文档+讲解等)/自行车管理软件/仓库管理软件/自行车仓储系统/仓库管理系统/自行车库存管理/自行车管理平台/仓库信息化系统
  • 基于Java+SpringBoot+SSM课程评价管理系统(源码+LW+调试文档+讲解等)/课程评估管理系统/课程反馈管理系统/课程管理评价系统/课程质量评价系统/教学评价管理系统
  • 大数据共享中的数据虚拟化技术解析
  • 数据产品需求文档(PRD)怎么写?阿里P8分享的模板+5个避坑点
  • 2026年2月宿州西装套裙实力厂家综合评测与选型指南 - 2026年企业推荐榜
  • 提示工程架构师教你:Agentic AI个性化对话生成在多场景的应用技巧
  • 进出口贸易集团SAP蓝图解决方案
  • FaceRecon-3D效果优化:基于OpenCV的图像增强技术应用
  • 【大家的项目】Yororen UI - 基于 Zed 的 gpui 框架的 Rust GUI 组件库,50+ 组件开箱即用
  • Qwen3-4B-Instruct-2507前端集成:Chainlit自定义组件实战