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

【C++】C++中的文件IO

C++中的文件IO

  • 1. 相关的类
  • 2.相关的成员函数
    • (1) 打开文件
      • 写法一:open
      • 写法二:fout
    • (2) 读取文件
      • 写法一: get
        • 示例代码:get读取文件
      • 写法二:read
        • 示例代码:文件的读取
        • 示例代码:文件的循环读取
      • 写法三: getline //读取一行数据
        • 示例代码:
    • (3) 写入文件
      • 写法一:put
      • 写法二:write
        • 示例代码:文件的写入
      • (4)文件关闭
      • (5)设置读写偏移--》随机读写文件(不按照顺序)
      • (6)如何判断文件打开/新建是否成功
      • (7)gcount()方法获取读到的字节数
        • 示例代码:

1. 相关的类

#include <fstream>
fstream      //跟文件的读写有关 ifstream  //input file stream读取文件读取文件用到的一些函数,其实cin也使用了(ifstream是istream的子类)ofstream //output file stream写入文件写入文件用到的一些函数,其实cout也使用了(ofstream是ostream的子类)

2.相关的成员函数

(1) 打开文件

写法一:open

ifstream   fin;
fin.open();  //没有新建功能,文件不存在打开就失败
void open (const char* filename,  ios_base::openmode mode = ios_base::in);参数:  filename --》路径名mode --》ios_base::in  可读ios_base::out  可写

写法二:fout

ofstream fout("1.txt");  //可以不用调用open,创建对象的同时,顺便把文件打开(清空打开,会清空源文件)//具备新建功能
或者: 
ofstream fout;fout.open("1.txt",ios_base::out)    会清空源文件fout.open("1.txt",ios_base::out|ios_base::in)

(2) 读取文件

写法一: get

istream& get (char& c);
istream& get (char* s, streamsize n); //只读第一行
示例代码:get读取文件
#include <iostream>
#include <fstream>  //跟文件IO有关的头文件
#include "myhead.h"
using namespace std;/*文件的读取
*/int main()
{char buf[5]={0};int ret;//创建ifstream的对象,通过这个对象来调用方法实现文件的读取  ifstream in;//打开你要读取的文件in.open("./1.txt");if(!in.is_open()){cout<<"打开文件失败"<<endl;}//一个个字节读取//ret=in.get();bzero(buf,5);in.get(buf,5);cout<<"刚才读取的内容是: "<<buf<<endl;in.get(buf,5);cout<<"刚才读取的内容是: "<<buf<<endl;in.get(buf,5);cout<<"刚才读取的内容是: "<<buf<<endl;in.get(buf,5);cout<<"刚才读取的内容是: "<<buf<<endl;in.get(buf,5);cout<<"刚才读取的内容是: "<<buf<<endl;//关闭文件in.close();return 0;
}

写法二:read

read (char* s, streamsize n);
fin.read(); //读取多个字节
示例代码:文件的读取
#include <iostream>
#include <fstream>  //跟文件IO有关的头文件using namespace std;int main()
{//创建ifstream的对象,通过这个对象来调用方法实现文件的读取  ifstream in;//打开你要读取的文件in.open("./1.txt");if(!in.is_open()){cout<<"打开文件失败"<<endl;}//读取文件内容char buf[100]={0};in.read(buf,100);cout<<"刚才读取的内容是: "<<buf<<endl;//关闭文件in.close();return 0;
}
示例代码:文件的循环读取
#include <iostream>
#include <fstream>  //跟文件IO有关的头文件
#include "myhead.h"using namespace std;int main()
{char buf[100]={0};//创建ifstream的对象,通过这个对象来调用方法实现文件的读取  ifstream in;//打开你要读取的文件in.open("./1.txt");if(!in.is_open()){cout<<"打开文件失败"<<endl;}while(1){//读取文件内容bzero(buf,100);in.read(buf,100);cout<<"刚才读取的内容是: "<<buf<<endl;if(in.eof())break;}//关闭文件in.close();return 0;
}

写法三: getline //读取一行数据

istream& getline (char* s, streamsize n );  //回车作为行的结束
istream& getline (char* s, streamsize n, char delim );第三个参数:delim 让程序员指定新的字符作为行结束的标记如果你没有指定,默认把回车换行作为行结束的标记
示例代码:
#include <iostream>
#include <fstream>  //跟文件IO有关的头文件
#include "myhead.h"
using namespace std;int main()
{char buf[100]={0};//创建ifstream的对象,通过这个对象来调用方法实现文件的读取  ifstream in;//打开你要读取的文件in.open("./1.txt");if(!in.is_open()){cout<<"打开文件失败"<<endl;}while(1){//按行读取文件内容bzero(buf,100);in.getline(buf,100);//in.getline(buf,100,'@');  //指定@作为读取结束标记cout<<"刚才读取的内容是: "<<buf<<endl;if(in.eof())break;}//关闭文件in.close();return 0;
}

(3) 写入文件

写法一:put

ostream& put (char c);

写法二:write

ostream& write (const char* s, streamsize n);
示例代码:文件的写入
#include <iostream>
#include <fstream>  //跟文件IO有关的头文件
#include "myhead.h"
using namespace std;/*文件的写入
*/int main()
{char buf[5]={0};int ret;//创建ofstream的对象,通过这个对象来调用方法实现文件的写入 //写法1:新建对象的时候,传递文件路径名(具备新建,清空覆盖源文件的功能,不需要调用open)//ofstream out("./1.txt");//写法2:ofstream out;out.open("./1.txt");//写入一个字节out.put('#');//写入多个字节out.write("nihao",3);//关闭文件out.close();return 0;
}

(4)文件关闭

fin.close();

(5)设置读写偏移–》随机读写文件(不按照顺序)

lseek()  fseek() --->linux
设置读的偏移  seekg()
设置写的偏移  seekp()istream& seekg (streamoff off, ios_base::seekdir way);参数: off --》偏移多少字节way --》从哪里开始偏移ios_base::beg   //起始位置  SEEK_SETios_base::cur   //当前位置  SEEK_CURios_base::end //末尾位置    SEEK_ENDostream& seekp (streamoff off, ios_base::seekdir way);

(6)如何判断文件打开/新建是否成功

方法一:

bool is_open();

方法二:

对象判断   if(对象)

(7)gcount()方法获取读到的字节数

streamsize gcount() const;
示例代码:
#include <iostream>
#include <fstream>  //跟文件IO有关的头文件using namespace std;/*设置读取文件的偏移
*/int main()
{//创建ifstream的对象,通过这个对象来调用方法实现文件的读取  ifstream in;//打开你要读取的文件in.open("./1.txt");//写法1://if(!in.is_open())//写法2:if(!in){cout<<"打开文件失败"<<endl;return -1;}//设置读取的偏移in.seekg(5,ios_base::beg); //起始位置往后偏移5个字节//读取文件内容--》从第6个字节开始读取char buf[100]={0};in.read(buf,100);//我想知道read读取了多少字节的数据cout<<"刚才读取的真实字节数: "<<in.gcount()<<endl;cout<<"刚才读取的内容是: "<<buf<<endl;//关闭文件in.close();return 0;
}
http://www.dtcms.com/a/585868.html

相关文章:

  • wordpress手机站如何做负面口碑营销案例
  • 谷歌黑客语法挖掘 SQL 注入漏洞
  • ps做网站logo青海做网站多少钱
  • Qt开发——环境搭建
  • 32HAL——RTC时钟
  • C#知识补充(一)——ref和out、成员属性、万物之父和装箱拆箱、抽象类和抽象方法、接口
  • 专业的设计网站建设网站做地区定位跳转
  • 网站做三层结构南京建设网站哪家好
  • pyautocad 获取选择线段的近似最小包围盒 (OBB) 三分搜索
  • Git Commit 高频提示详解:用户名邮箱配置及其他常见提示解决方案
  • 打开网站图片弹入指定位置代码网络域名备案查询
  • 豆包 Spring 常用注解详解及分类
  • 企业网站建设收费最大的网站开发公司
  • 服务器运维(六)跨域配置 Preflight 问题——东方仙化神期
  • 第三次作业-第四章网站搭建
  • React 17
  • Linux:多路转接
  • 为什么国内禁用docker呢?
  • 石家庄行业网站深圳建筑工地招工招聘信息
  • 云溪网络建站宝盒wordpress发文章套模版
  • 人在虚弱的时候真的能看到鬼
  • zabbix原生高可用集群应用实战
  • flink1.20.2环境部署和实验-1
  • 网站主目录程序开发步骤不包括
  • 云手机技术是如何实现的?
  • 现有rest api转换为MCP工具 存量api改造为MCP Server
  • MyBatis:性能优化实战 - 从 SQL 优化到索引设计
  • 【Golang】常见数据结构原理剖析
  • 做百度推广得用网站是吗做小说网站做国外域名还是国内的好处
  • Ubuntu 复制王者:rsync -av 终极指南 —— 进度可视化 + 无损同步,效率甩 cp 几条街!