C++读写二进制文件块

本文介绍了一个简单的C++程序示例,该程序用于读取并复制STL格式的文件。通过使用标准输入输出流类`ifstream`和`ofstream`,程序能够打开指定的STL文件,并将其完整内容复制到另一个同名文件中。此过程包括检查文件是否成功打开、获取文件大小、分配内存缓冲区以及读写操作。

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

#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
ofstream outfile;
infile.open("**.stl",ios_base::binary);
if (!infile.is_open())
{
cout<<"file can not open";
exit(EXIT_FAILURE);
}
outfile.open("**.stl",ios_base::binary);
infile.seekg(0,ios_base::end);
streampos len=infile.tellg();
cout<<len<<endl;
infile.seekg(0);
char *buffer=new char [len];
infile.read(buffer,len);
outfile.write(buffer,len);
infile.close();
outfile.close();
delete [] buffer;
return 0;


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值