文章目录

  • IO: 向设备输入数据和输出数据C++的IO流
    • 写文件
    • 读文件
    • 对二进制文件流读写
    • 写二进制文件
    • 读二进制文件
    • 对文件流按格式读写取数据
    • 按指定格式读文件
    • 文件流的状态检查
    • 文件流的定位
    • 获取当前文件的长度
    • seekp设置该输出流的位置
    • 计算机英语
    • 常见错误总结

IO: 向设备输入数据和输出数据C++的IO流


c++中,必须通过特定的已经定义好的类, 来处理IO(输入输出)

文件流: 对文件进行读写操作
头文件:
类库:
ifstream 对文件输入(读文件)
ofstream 对文件输出(写文件)
fstream 对文件输入或输出

写文件

#include <fstream>
#include <iostream>
#include <string>using namespace std;int main()
{string name;int age;ofstream outfile;  //也可以使用fstream, 但是fstream的默认打开方式不截断文件长度// ofstream的默认打开方式是,  截断式写入 ios::out |  ios::trunc// fstream的默认打开方式是,  截断式写入   ios::out// 建议指定打开方式outfile.open("user.txt", ios::out | ios::trunc);while (1) {cout << "请输入姓名: [ctrl+z退出] ";cin >> name;if (cin.eof()) { //判断文件是否结束break;}outfile << name << "\t";cout << "请输入年龄: ";cin >> age;  outfile << age << endl;  //文本文件写入}// 关闭打开的文件outfile.close();system("pause");return 0;
}

读文件

#include <fstream>
#include <iostream>
#include <string>using namespace std;int main()
{string name;int age;ifstream infile;infile.open("user.txt");while (1) {infile >> name;if (infile.eof()) { //判断文件是否结束break;}cout << name << "\t";infile >> age;cout << age << endl;  }// 关闭打开的文件infile.close();system("pause");return 0;
}

对二进制文件流读写

文本文件和二进制文件的区别?

文本文件: 写数字1, 实际写入的是 ‘1’
二进制文件:写数字1, 实际写入的是 整数1(4个字节,最低字节是1, 高3个字节都是0)
写字符‘R’实际输入的还是‘R’
写二进制文件
使用文件流对象的write方法写入二进制数据.

写二进制文件

#include <fstream>
#include <iostream>
#include <string>using namespace std;int main()
{string name;int age;ofstream outfile;outfile.open("user.dat", ios::out | ios::trunc | ios::binary);while (1) {cout << "请输入姓名: [ctrl+z退出] ";cin >> name;if (cin.eof()) { //判断文件是否结束break;}outfile << name << "\t";cout << "请输入年龄: ";cin >> age;  //outfile << age << endl;  //会自动转成文本方式写入outfile.write((char*)&age, sizeof(age));}// 关闭打开的文件outfile.close();system("pause");return 0;
}

读二进制文件

使用文件流对象的read方法.

#include <fstream>
#include <iostream>
#include <string>using namespace std;int main()
{string name;int age;ifstream infile;infile.open("user.dat", ios::in | ios::binary);while (1) {infile >> name;if (infile.eof()) { //判断文件是否结束break;}cout << name << "\t";// 跳过中间的制表符char tmp;infile.read(&tmp, sizeof(tmp)); //infile >> age; //从文本文件中读取整数, 使用这个方式infile.read((char*)&age, sizeof(age));cout << age << endl;  //文本文件写入}// 关闭打开的文件infile.close();system("pause");return 0;
}

对文件流按格式读写取数据

使用stringstream

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>using namespace std;int main()
{string name;int age;ofstream outfile;outfile.open("user.txt", ios::out | ios::trunc);while (1) {cout << "请输入姓名: [ctrl+z退出] ";cin >> name;if (cin.eof()) { //判断文件是否结束break;}cout << "请输入年龄: ";cin >> age;stringstream s;s << "name:" << name << "\t\tage:" << age << endl;outfile << s.str();}// 关闭打开的文件outfile.close();system("pause");return 0;
}

按指定格式读文件

没有优雅的C++解决方案, 使用C语言的sscanf

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <Windows.h>using namespace std;int main(void)
{char name[32];int age;string line;ifstream infile;infile.open("user.txt");while (1) {getline(infile, line);if (infile.eof()) { //判断文件是否结束break;}sscanf_s(line.c_str(), "姓名:%s 年龄:%d", name, sizeof(name),&age);cout << "姓名:" << name << "\t\t年龄:" << age << endl;}infile.close();system("pause");return 0;
}

文件流的状态检查

s.is_open( )
文件流是否打开成功,

s.eof( ) 流s是否结束

s.fail( )
流s的failbit或者badbit被置位时, 返回true
failbit: 出现非致命错误,可挽回, 一般是软件错误
badbit置位, 出现致命错误, 一般是硬件错误或系统底层错误, 不可挽回

s.bad( )
流s的badbit置位时, 返回true

s.good( )
流s处于有效状态时, 返回true

s.clear( )
流s的所有状态都被复位

文件流的定位

seekg( off_type offset, //偏移量
ios::seekdir origin ); //起始位置
作用:设置输入流的位置
参数1: 偏移量
参数2: 相对位置
beg 相对于开始位置
cur 相对于当前位置
end 相对于结束位置

读取当前程序的最后50个字符

#include <iostream>
#include <fstream>
#include <string>using namespace std;int main(void) {ifstream infile;infile.open("定位.cpp");if (!infile.is_open()) {return 1;}infile.seekg(-50, infile.end);while (!infile.eof()) {string line;getline(infile, line);cout << line << endl;}infile.close();system("pause");return 0;
}

//tellg返回该输入流的当前位置(距离文件的起始位置的偏移量)

获取当前文件的长度

#include <iostream>
#include <fstream>
#include <string>using namespace std;int main(void) {ifstream infile;infile.open("定位.cpp");if (!infile.is_open()) {return 1;}// 先把文件指针移动到文件尾infile.seekg(0, infile.end);int len = infile.tellg();cout << "len:" << len;infile.close();system("pause");return 0;
}

seekp设置该输出流的位置

先向新文件写入:“123456789”
然后再在第4个字符位置写入“ABC”

#include <iostream>
#include <fstream>
#include <string>using namespace std;int main(void) {ofstream outfile;outfile.open("test.txt");if (!outfile.is_open()) {return 1;}outfile << "123456789";outfile.seekp(4, outfile.beg);outfile << "ABC";outfile.close();system("pause");return 0;
}

计算机英语

io input output的简写
输出输出
ios C++ 输出输出流的基类
stream 流
istream 输入流
ostream 输出流
iostream 输出输出流
fstream file stream的简写 文件流
ifstream 文件输入流
ofstream 文件输出流
stringstream 字符串流
istringstream 字符串输入流
ostringstream 字符串输出流
seek 寻找
seekg 设置输入流的位置
seekp 设置输出流的位置
tell 告诉
tellg 返回当前流的位置
open 打开
is_open 是否打开成功
eof end of file的简写
文件结束
good 好的
bad 坏的
fail 失败

常见错误总结

1.文件没有关闭
文件没有关闭, close(),可能导致写文件失败
2.文件打开方式不合适
3.在VS2015的部分版本中,当sscanf和sscanf_s的格式字符串中含有中文时,可能会读取失败。
在vs2019中未发现该类问题。

C++ I/O文件读写操作相关推荐

  1. java大文件读写操作

    转载自:http://blog.csdn.net/akon_vm/article/details/7429245 RandomAccessFile RandomAccessFile是用来访问那些保存数 ...

  2. C++ builder 的文件读写操作总结

    C++ builder 的文件读写操作总结 在编程的过程中,文件的操作是一个经常用到的问题,在C++Builder中,可以使用多种方法对文件操作,下面我就按以下几个部分对此作详细介绍,就是: 1.基于 ...

  3. unity webgl读写txt文件_python Files文件读写操作

    今天学习python的Files文件读写操作,并记录学习过程欢迎大家一起交流分享. 首先新建一个文本文件test.txt,内容如下: hello worldhello youhello mehello ...

  4. java文件读写操作类

    借鉴了项目以前的文件写入功能,实现了对文件读写操作的封装 仅仅需要在读写方法传入路径即可(可以是绝对或相对路径) 以后使用时,可以在此基础上改进,比如: 写操作: 1,对java GUI中文本框中的内 ...

  5. OpenCV学习笔记(四):XML,YAML(.txt,.doc)文件读写操作

    OpenCV学习笔记(四):XML,YAML(.txt,.doc)文件读写操作 一.Write_XML_and_YAML_File(写入XML) #include <opencv2/opencv ...

  6. Python 文件读写操作-Python零基础入门教程

    目录 一.Python 文件的打开 open 二.Python 文件的关闭 close 三.Python 文件的读取 read 1.read 函数 2.readline 函数 3.readlines ...

  7. 【转】Android - 文件读写操作 总结

    Android - 文件读写操作 总结 原文出处:http://blog.csdn.net/ztp800201/article/details/7322110 在android中的文件放在不同位置,它 ...

  8. php中文件读写总结,PHP中文件读写操作

    PHP中文件读写操作 PHP中提供了一系列的I/O函数,能简捷地实现我们所需要的功能,包括文件系统操作和目录操作(如"复制[copy]").下面给大家介绍的是基本的文件读写操作:( ...

  9. C/C++ 文件读写操作总结

    C/C++ 文件读写操作总结 在编程的过程中,文件的操作是一个经常用到的问题,在C++Builder中,可以使用多种方法对文件操作,下面我就按以下几个部分对此作详细介绍,就是: 1.基于C的文件操作: ...

  10. IOS 文件读写操作详解及简单实例

    iPhone 文件读写操作 1,写文件操作 - (IBAction)btnWrite:(id)sender {//创建文件管理器NSFileManager *fileManager = [NSFile ...

最新文章

  1. Docker 学习资源整理
  2. 2个字符串相等 swift_Swift快速入门(一)之 Swift基础语法
  3. JQuery Datatables 在Bootstrap tab中列名无法对齐的问题
  4. coredump产生与分析
  5. 设计模式002:简单工厂模式
  6. Python实现大自然数分解为最多4个平方数之和(1)
  7. ubuntu16.0.4安装mysql5.7以及设置远程访问
  8. Python2+Selenium入门04-关于WebDriver类
  9. yolov3从头实现(六)损失计算
  10. Kaggle/Titanic python分析和建模
  11. Java之品优购课程讲义_day05(4)
  12. iOS 中 常用的第三方库
  13. 【单目标优化求解】基于matlab竞争学习的粒子群算法求解单目标优化问题【含Matlab源码 1784期】
  14. 微信小程序-基于云开发 CMS + Vant Weapp 电商 Demo 来了!
  15. n1盒子当无线打印服务器,n1下ubuntu安装cups配置airprint网络打印服务器
  16. 网吧相关法律期待完善
  17. 蓝牙中的三种音频编码:Apt-X、SBC、AAC,请问分别有什么区别?
  18. java流量监控系统demo_搭建一个简单的基于web的网络流量监控可视化系统
  19. 论文阅读:Ultra Wide View Based Panoramic VR Streaming
  20. 分享一个项目平台原型图

热门文章

  1. 不用linux转录组数据分析,玩转RNA-seq数据也可以不需要linux ?
  2. 远程计算机强迫关闭一个连接,远程主机强迫关闭了一个现有的连接。
  3. 软件项目管理作业(一)
  4. 计算机如何执行一条机器指令
  5. hazelcast 搭建_Spring Boot集成Hazelcast实现集群与分布式内存缓存
  6. 中级微观经济学:Chap 32 交换
  7. 小程序Canvas绘制图片太大,自动闪退,安卓会有crash问题
  8. 数据分析研究思维导图
  9. python句柄无效_作为Windows服务运行的Python:OSError:[WinError 6]句柄无效
  10. 络腮胡子的男程序员们都在怎么刮胡子呢?应该用电推子来刮胡子