首页 > 解决方案 > C ++打开具有2个约束的文件

问题描述

在 C++ 中,我想用记事本打开我的文本文件,但是:

可能吗 ?

我有这个: _popen("notepad.exe C:\X\X\X.txt", "r"); 但它会打开一个 cmd 选项卡。

标签: c++windows

解决方案


(仅限 Windows 的解决方案)

通过修改示例:https ://docs.microsoft.com/en-us/windows/win32/procthread/creating-processes

对于 C++:

#include <iostream>
#include <Windows.h>

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        std::cout << "Usage: " << argv[0] << " [cmdline]\n";
        return EXIT_FAILURE;
    }

    STARTUPINFOA        si = {sizeof(si)};
    PROCESS_INFORMATION pi = {};

   // Start the child process.
    if (!CreateProcessA(nullptr, // No module name (use command line)
                        argv[1], // Command line
                        nullptr, // Process handle not inheritable
                        nullptr, // Thread handle not inheritable
                        false,   // Set handle inheritance to FALSE
                        0,       // No creation flags
                        nullptr, // Use parent's environment block
                        nullptr, // Use parent's starting directory
                        &si,     // Pointer to STARTUPINFO structure
                        &pi))    // Pointer to PROCESS_INFORMATION structure
    {
        std::cout << "CreateProcess failed (" << GetLastError() << ").\n";
        return EXIT_FAILURE;
    }

    // Wait until child process exits.
    //WaitForSingleObject(pi.hProcess, INFINITE);

    // Close process and thread handles.
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

    return EXIT_SUCCESS;
}

推荐阅读