64,687
社区成员
发帖
与我相关
我的任务
分享
char i;
......
infile.read((char *)&i,sizeof(int));
outfile.write((char *)&i,sizeof(int));
复制文件不需要一个字节一个字节的处理,先获取文件的大小,分配内存,然后一次性读入就可以了。
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
char i;
char source[256];
char destination[256];
cout<<"输入源文件"<<endl;
cin>>source;
cout<<"输入目标文件"<<endl;
cin>>destination;
ifstream infile(source,ios::binary);
if(! infile)
{
cerr<<"no found "<<endl;
exit(1);
}
ofstream outfile(destination,ios::binary);
while(1)
{
infile.read((char *)&i,sizeof(int));
outfile.write((char *)&i,sizeof(int));
if(infile.eof())
break;
}
infile.close();
outfile.close();
return 0;
}
输入源文件
a.jpg
输入目标文件
b.jpg
while(1)
{
infile.read((char *)&i,sizeof(int));
outfile.write((char *)&i,sizeof(int));
if(infile.eof()) // 这里改成infile
break;
}
还有注意目标文件名应该有相应的后缀。