使用C ++将一个文件的内容复制到另一个文件 [英] Copying contents of one file to another in C++

查看:827
本文介绍了使用C ++将一个文件的内容复制到另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的程序试图将文件src的内容复制到另一个,dest,在C + +。简化代码如下:

  #include< fstream> 
using namespace std;
int main()
{
fstream src(c:\\tplat\test\\secClassMf19.txt,fstream :: binary);
ofstream dest(c:\\tplat\\test\\mf19b.txt,fstream :: trunc | fstream :: binary);
dest<< src.rdbuf();
return 0;
}



当在Windows中使用GCC Compiler使用CODEBLOCKS ide构建和执行程序时,创建了一个名为.... mf19.txt的新文件,但没有数据被复制到其中,而filesize = 0kb。我有积极的,我有一些数据在... secClassMf19.txt。



我遇到同样的问题,当我编译相同的progeam在Windows Visual C + + 2008。 / p>

任何人都可以帮助解释为什么我得到这个意想不到的行为,更重要的是如何解决这个问题?

解决方案

您需要检查在使用这些流之前打开文件是否成功。此外,它永远不会伤害,检查是否一切都顺利。将您的代码更改为此并返回:

  int main()
{
std :: fstream src(c:\\tplat\test\\secClassMf19.txt,std :: ios :: binary);
if(!src.good())
{
std :: cerr< 错误打开输入文件\\\
;
std :: exit(1);
}
std :: ofstream dest(c:\\tplat\\test\\mf19b.txt,std :: ios :: trunc | std :: ios: :binary);
if(!destgood())
{
std :: cerr< error opening output file\;
std :: exit(2);
}
dest<< src.rdbuf();
if(!src.eof())
std :: cerr<< 从文件读取失败\\\
;
if(!dst.good())
std :: cerr<< 写入文件失败\\\
;
return 0;
}



我猜你会报告前两个支票中的一个。



如果打开输入文件失败,请尝试使用 std :: ios :: in | std :: ios :: binary 而不是 std :: ios :: binary


I am using the following program to try to copy the contents of a file, src, to another, dest, in C++. The simplified code is given below:

#include <fstream>
using namespace std;
int main()
{
  fstream src("c:\\tplat\test\\secClassMf19.txt", fstream::binary);
  ofstream dest("c:\\tplat\\test\\mf19b.txt", fstream::trunc|fstream::binary);
  dest << src.rdbuf();
  return 0;
}

When I built and executed the program using CODEBLOCKS ide with GCC Compiler in windows, a new file named "....mf19.txt" was created, but no data was copied into it, and filesize = 0kb. I am positive I have some data in "...secClassMf19.txt".

I experience the same problem when I compiled the same progeam in windows Visual C++ 2008.

Can anyone please help explain why I am getting this unexpected behaviour, and more importantly, how to solve the problem?

解决方案

You need to check whether opening the files actually succeeds before using those streams. Also, it never hurts to check if everything went right afterwards. Change your code to this and report back:

int main()
{
    std::fstream src("c:\\tplat\test\\secClassMf19.txt", std::ios::binary);
    if(!src.good())
    {
        std::cerr << "error opening input file\n";
        std::exit(1);
    }
    std::ofstream dest("c:\\tplat\\test\\mf19b.txt", std::ios::trunc|std::ios::binary);
    if(!dest.good())
    {
        std::cerr << "error opening output file\n";
        std::exit(2);
    }
    dest << src.rdbuf();
    if(!src.eof())
        std::cerr << "reading from file failed\n";
    if(!dst.good())
        std::cerr << "writing to file failed\n";
    return 0;
}

I bet you will report that one of the first two checks hits.

If opening the input file fails, try opening it using std::ios::in|std::ios::binary instead of just std::ios::binary.

这篇关于使用C ++将一个文件的内容复制到另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆