【发布时间】:2011-01-09 15:35:28
【问题描述】:
我有以下代码,它工作得很好(除了它很慢,但我不太在意)。将 infile 的全部内容写入 outfile 似乎并不直观。
// Returns 1 if failed and 0 if successful
int WriteFileContentsToNewFile(string inFilename, string outFilename)
{
ifstream infile(inFilename.c_str(), ios::binary);
ofstream outfile(outFilename.c_str(), ios::binary);
if( infile.is_open() && outfile.is_open() && infile.good() && outfile.good() )
{
outfile << infile.rdbuf();
outfile.close();
infile.close();
}
else
return 1;
return 0;
}
有什么见解吗?
【问题讨论】:
-
我要补充一点,不需要显式调用
close()。无论如何,析构函数都会做同样的事情。这样可以节省一些行。 ;)