复制文件 [英] copying a file
问题描述
当我运行以下代码时
ifstream fin(" scenario.chk",ios :: in | ios :: binary);
ofstream fout(" out.chk",ios :: out | ios :: binary);
while(!fin.eof())
{
fout.put(fin.get());
}
fin.close();
fout.close();
文件大小似乎有一个字节差异...
复制文件似乎也似乎因为它而腐败。
为什么这不起作用?并且
是否有更好的方法来复制文件?
(注意我不想运行复制文件命令...我将
插入中间的东西)
when i run the following code
ifstream fin("scenario.chk",ios::in | ios::binary);
ofstream fout("out.chk",ios::out | ios::binary);
while( !fin.eof() )
{
fout.put(fin.get());
}
fin.close();
fout.close();
there seems to be a one byte difference in filesize...
also the copied file seems to be corrupt because of it.
why doesn''t this work? and
is there a better way to copy the file?
(note that i dont want to run a "copy file" command... i will be
inserting stuff in the middle)
推荐答案
呃......使用更简单的测试文件。它实际上附加了额外的
字符..我只是不确定为什么。我猜..它需要尝试阅读
另一个字符才被标记为文件结尾。
uhh..using a simpler test file.. it does in fact append an extra
character.. i''m just not sure why. i guess.. it needs to try reading
another character before it is flagged as end of file.
it''因为我正在读二进制文字不是吗?
?将是eof角色...
问题是..我如何检查它?
it''s because i''m reading and writing in binary isn''t it?
? would be the eof character...
question is.. how do i check for it?
嗯......是否有一个比
更有效的解决方案(fin.good())
{
c = fin.get() ;
if(c!= EOF)fout.put(c);
}
并且必须检查每个角色看它是否是EOF?
uhhm... is there a more efficient solution than
while( fin.good() )
{
c = fin.get();
if( c != EOF ) fout.put(c);
}
and having to check every character to see if its the EOF?
这篇关于复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!