Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在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; } #包括 使用名称空间std; int main() { fstream src(“c:\\tplat\test\\secClassMf19.txt”,fstream::binary); 流dest(“c:\\tplat\\test\\mf19b.txt”,fstream::trunc | fstream::binary); dest_C++_Windows_File_Copy - Fatal编程技术网

在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; } #包括 使用名称空间std; int main() { fstream src(“c:\\tplat\test\\secClassMf19.txt”,fstream::binary); 流dest(“c:\\tplat\\test\\mf19b.txt”,fstream::trunc | fstream::binary); dest

在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; } #包括 使用名称空间std; int main() { fstream src(“c:\\tplat\test\\secClassMf19.txt”,fstream::binary); 流dest(“c:\\tplat\\test\\mf19b.txt”,fstream::trunc | fstream::binary); dest,c++,windows,file,copy,C++,Windows,File,Copy,在使用这些流之前,您需要检查打开文件是否确实成功。此外,检查之后是否一切正常也无妨。将您的代码更改为此,然后报告: 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)

在使用这些流之前,您需要检查打开文件是否确实成功。此外,检查之后是否一切正常也无妨。将您的代码更改为此,然后报告:

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;
}
intmain()
{
std::fstream src(“c:\\tplat\test\\secClassMf19.txt”,std::ios::binary);
如果(!src.good())
{

你有什么理由不使用这个函数吗


最好的

正如本文所述,您的
src
实例是一个常规的fstream,并且您没有为输入指定开放模式。简单的解决方案是将
src
作为
ifstream
的实例,您的代码就可以工作了。(只需添加一个字节!)

如果您测试了输入流(正如sbi所建议的),您会发现它没有正确打开,这就是目标文件大小为零的原因。它是在写入模式下打开的(因为它是流的
),使用截断选项将其设置为零,但写入
rdbuf()的结果
只是失败了,没有写入任何内容

另一件需要注意的事情是,虽然这对小文件很有效,但对大文件来说效率很低。也就是说,您正在将源文件的全部内容读入内存,然后在一个大数据块中再次写入。这会浪费大量内存。您最好分块读取(例如1MB,一个磁盘缓存的合理大小)一次写入一个块,最后一个块是该大小的剩余部分。要确定源的大小,您可以搜索到底并查询文件偏移量,然后知道您正在处理的字节数


如果您使用本机API,您可能会发现您的操作系统在复制文件方面更加高效,但它的可移植性会降低。您可能需要查看文件系统模块以获得可移植的解决方案。

欢迎访问stackoverflow.com。下次,请使用编辑窗口顶部的
101010
按钮格式化您的代码。此外,您可能想阅读。当您说您肯定在secClassMf19.txt中有数据时,您是否真的在记事本中打开了它并进行了查看?我尝试了sbi的建议,它显示了读取输入文件的错误(retuen 1)。我尝试了sbi的建议(使用std::ios::in | std::ios::binary而不是std::ios::binary),程序仍然无法读取输入文件(返回1)。即使按照gavinb yiels的建议使用ifstream,也没有什么乐趣。我刚刚看到Vagaus suggestion wrt.CopyFile。但我之前已经尝试过了(添加#include。我当时使用的代码是:CopyFile((LPCWSTR)“c:\\tplat\test\\secClassMf19.txt”,(LPCWSTR)“c:\\tplat\\test\\mf19b.txt”,true);但它也不起作用。该文件在记事本中打开,大小只有1kb。(请注意,如果您的用户名不以至少三个字母数字字符开头,则在使用@username语法的注释中对您进行寻址时,您不会收到通知。您可以在此处看到这一点,因为它已添加到您问题的答案中,但在任何其他问题中,当有人答复您的某个注释时,它不会显示在您的注释中。)“响应”选项卡。)“tplat”和“test”之间缺少一个双反斜杠"在您的源文件名中。因此,您会收到一个错误代码,告诉您找不到该文件。如果改用正斜杠,它会很好地工作,在POSIX系统上也可以工作,并且更易于阅读。我在OS X上测试了我的修改,效果很好。@T J:删除测试,因为它们从未命中,就像删除栏杆一样,因为没有body曾经倒下过。我尝试过sbi的建议(使用std::ios::in | std::ios::binary而不是std::ios::binary),但程序仍然无法读取输入文件(返回1)。即使按照gavinb yiels的建议使用ifstream也没有什么乐趣。我刚刚看到Vagaus建议wrt.CopyFile。但我之前尝试过(添加#include。我当时使用的代码是:CopyFile((LPCWSTR)“c:\\tplat\test\\secClassMf19.txt”,(LPCWSTR)“c:\\tplat\\test\\mf19b.txt”,true);但它也不起作用。也许我做错了什么。