c++中的文件io

文章详细介绍了C++中fstream库的使用,包括ifstream子类用于读取文件,ofstream子类用于写入文件。通过创建对象,设置打开模式,然后调用read、getline、write等方法进行文件的读写。同时提到了大文件分段读取拷贝的示例。

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

1.涉及到的类和方法
              fstream--》子类 ifstream  跟文件的读取有关
                                子类ofstream  跟文件的写入有关
             #include<fstream>

   2.读取文件
         (1)创建 ifstream的对象
                ifstream();
    ifstream (const char* filename, ios_base::openmode mode = ios_base::in);
                              参数: filename --》你要读取的文件路径名
                                         mode --》权限
                                                               ios_base::in   可读     O_RDONLY
                                                               ios_base::out 可写     O_WRONLY
                                                               ios_base::trunc 覆盖   O_TRUNC
         (2)打开文件
                 void open (const char* filename,  ios_base::openmode mode = ios_base::in);

         (3)读取文件
                 istream& read (char* s, streamsize n);
                 istream& getline (char* s, streamsize n );   //一次最多只能读取一行数据,类似于fgets()
                 istream& getline (char* s, streamsize n, char delim );  //参数delim作为读取一行的分界符(默认getline以回车作为一行的分界符)
                 istream& get (char& c);  //读取一个字符
     istream& get (char* s, streamsize n);  //读取n个字符

         (4)关闭文件
                 void close();    

   3.写入文件
        (1)创建ofstream对象    
                ofstream();
                ofstream (const char* filename, ios_base::openmode mode = ios_base::out);

        (2)打开文件
                 void open (const char* filename,  ios_base::openmode mode = ios_base::in); 

        (3)写入文件
                 ostream& write (const char* s, streamsize n);
    
         (4)关闭文件
                 void close(); 

   4.设置读写偏移
              linux--》lseek()/fseek()
              设置读的偏移:  istream& seekg (streamoff off, ios_base::seekdir way);
              设置写的偏移:  ostream& seekp (streamoff off, ios_base::seekdir way);
                                                   参数: off --》你打算偏移多少字节
                                                              way --》从哪个位置开始偏移
                                                                               ios_base::beg  //起始位置
                                                                               ios_base::cur   //当前位置    
                                                                               ios_base::end  //末尾位置

 写入文件使用ofstream的带参构造函数和无参构造函数

#include<iostream>
#include<fstream>

using namespace std;

int main()
{
	//创建ofstream的对象
	ofstream stream("./new.txt");
	
	//打开--》错误的ofstream stream("./new.txt")具备打开功能
	//stream.open("./new.txt");
	
	//写入内容
	stream.write("冬至吃汤圆",15);
	
	//关闭文件
	stream.close();
	return 0;
}
#include<iostream>
#include<fstream>

using namespace std;

int main()
{
	//创建ofstream的对象
	ofstream stream;
	
	//打开
	stream.open("./other.txt");
	
	//写入内容
	stream.write("新年快乐2022",16);
	
	//关闭文件
	stream.close();
	return 0;
}

 大文件分段读取拷贝

#include<iostream>
#include<fstream>

using namespace std;

int main()
{
	//定义缓冲区存放读取的内容
	char buf[1024*1024];  //1M
	
	//打开源文件
	ifstream in;
	in.open("./1.mp4");
	
	//新建目标文件
	ofstream out("./2.mp4");
	
	//循环读取,循环写入
	while(1)
	{
		//读取源文件
		in.read(buf,1024*1024); //5000
		//把读取的内容写入到目标文件中
		out.write(buf,in.gcount());
		//读到文件末尾就退出循环
		if(in.eof())
			break;
	}
	
	//关闭文件
	in.close();
	out.close();
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hqb_newfarmer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值