Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.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/azure/13.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++ C++;can';创建后,无法打开文件_C++ - Fatal编程技术网

C++ C++;can';创建后,无法打开文件

C++ C++;can';创建后,无法打开文件,c++,C++,我正在尝试制作一个程序,然后用这个代码打开它 //Make the file std::ifstream src(a, std::ios::binary); std::ofstream dst(b, std::ios::binary); dst << src.rdbuf(); //Execute it Execute((LPCTSTR)b.c_str()); 我已经测试了制作文件,当我手动打开文件时,它工作正常,没有任何问题。我尝试了execute函数,它工作得很好,没有任

我正在尝试制作一个程序,然后用这个代码打开它

//Make the file
std::ifstream  src(a, std::ios::binary);
std::ofstream  dst(b, std::ios::binary);

dst << src.rdbuf();

//Execute it
Execute((LPCTSTR)b.c_str());
我已经测试了制作文件,当我手动打开文件时,它工作正常,没有任何问题。我尝试了execute函数,它工作得很好,没有任何问题。但是,当我出于某种原因将这两个组合在一起时,它将不会执行

如果有人能告诉我为什么和/或如何修复它,那将非常有帮助


谢谢。

问题解决了,结果很简单,正如《杀手地带小子》所指出的,我只需关闭流。

不妨键入完整答案。基本上,如果流的
未关闭,则
createProcess
将失败。下面是要测试的示例代码:

#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>

bool Execute(LPCTSTR Process)
{
    STARTUPINFO sInfo = {};
    sInfo.cb = sizeof(sInfo);
    PROCESS_INFORMATION pInfo = {};

    return CreateProcess(Process, NULL, NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &sInfo, &pInfo);
}

int main()
{
    std::wstring src_name(L"C:\\Windows\\system32\\notepad.exe");
    std::wstring dst_name(L"C:\\Users\\KK\\Desktop\\mynotepad.exe");

    std::ifstream src(src_name, std::ios::binary);
    std::ofstream dst(dst_name, std::ios::binary);

    dst << src.rdbuf();

    src.close();
    dst.close(); // has to be closed before execution

    if (!Execute(dst_name.c_str()))
    {
        std::cout << "ERROR: " << GetLastError() << std::endl;
    }

    return 0;
}
#包括
#包括
#包括
#包括
bool执行(LPCTSTR进程)
{
STARTUPINFO sInfo={};
sInfo.cb=sizeof(sInfo);
进程信息pInfo={};
返回CreateProcess(Process,NULL,NULL,NULL,FALSE,CREATE\u DEFAULT\u ERROR\u MODE,NULL,NULL,&sInfo,&pInfo);
}
int main()
{
std::wstring src_name(L“C:\\Windows\\system32\\notepad.exe”);
std::wstring dst_name(L“C:\\Users\\KK\\Desktop\\mynotepad.exe”);
std::ifstream src(src_名称,std::ios::binary);
std::ofstreamdst(dst_名称,std::ios::binary);

dst编辑您的问题,并与调试器讨论您从单步执行代码中学到的知识。如果您遵循发布问题的步骤,人们更有可能回答您的问题。他们是认真的。您必须证明您在这方面付出了最小的努力—例如使用调试器,将代码剥离到本质以查看问题是否仍然存在,等等。请参阅指导原则。祝您好运。可能重复的
#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>

bool Execute(LPCTSTR Process)
{
    STARTUPINFO sInfo = {};
    sInfo.cb = sizeof(sInfo);
    PROCESS_INFORMATION pInfo = {};

    return CreateProcess(Process, NULL, NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &sInfo, &pInfo);
}

int main()
{
    std::wstring src_name(L"C:\\Windows\\system32\\notepad.exe");
    std::wstring dst_name(L"C:\\Users\\KK\\Desktop\\mynotepad.exe");

    std::ifstream src(src_name, std::ios::binary);
    std::ofstream dst(dst_name, std::ios::binary);

    dst << src.rdbuf();

    src.close();
    dst.close(); // has to be closed before execution

    if (!Execute(dst_name.c_str()))
    {
        std::cout << "ERROR: " << GetLastError() << std::endl;
    }

    return 0;
}