C++文件流有关问题,用ifstream infile(source,ios:binary);打不开文件
C++文件流问题,用ifstream infile(source,ios::binary);打不开文件
我想把E盘的名为11的图片复制到G盘,取名12.
可是测试后发现找不到E盘的11.谁帮我看看,顺便看下我后面的语句是不是对的。谢了
#include <iostream>
#include <fstream>
using namespace std;
void 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(outfile.eof)
break;
}
infile.close();
outfile.close();
}

------解决方案--------------------
我想把E盘的名为11的图片复制到G盘,取名12.
可是测试后发现找不到E盘的11.谁帮我看看,顺便看下我后面的语句是不是对的。谢了
#include <iostream>
#include <fstream>
using namespace std;
void 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(outfile.eof)
break;
}
infile.close();
outfile.close();
}
------解决方案--------------------
while(1)
{
infile.read((char *)&i,sizeof(int));
outfile.write((char *)&i,sizeof(int));
if(infile.eof()) // 这里改成infile
break;
}