【问题标题】:C++: Check if a file is runningC++:检查文件是否正在运行
【发布时间】:2021-03-16 09:33:00
【问题描述】:

这里非常菜鸟。 基本上我想做的是,一旦我运行我的程序,它就会打开一个 txt 文件。 然后开始一个 while 循环,只有在我关闭文件时才会结束。

如果可能的话,我知道这肯定比我在这里写的更复杂,但我希望能学到新的东西:)

#include <iostream>
#include <windows.h>
using namespace std;

main()
{
        system("test.txt"); //open my file

        while(ProcessIsRunning()) //???
        {
                cout << "Process is currently running...";
                Sleep(1000);
        }
        
        cout << "Process has stopped running"; //when I close my file, exits while and outputs this

}

【问题讨论】:

  • 您从哪里得知system("test.txt") 打开了一个文件?您的实际问题是“我想在不同的程序中打开一个文件并等到该程序关闭它”?
  • 很不清楚你在问什么,如果你想打开一个文件,看看std::fstream,但是你说的“关闭文件”是什么意思?你的意思是在你的程序内部关闭它,在外部关闭它?
  • @molbdnilo 显然它可以工作并打开我的 txt 文件。而我想做的是从我的程序中打开一个文件并让我的程序等到我打开的文件的进程停止运行
  • @cppnoob 在我看来,您误解了“读取文件”在应用程序中的工作方式,您似乎希望使用 Notepad/Visual Studio Code 之类的文本编辑器打开并显示文件你,在你等待它关闭的时候。在 C++ 中读取文件不是这样工作的,它只允许您将文件作为字节流读取。如果你想在外部打开它,你需要启动一个带有适当参数的子进程,它会在记事本/VS Code/任何你想要的文件中打开文件,并等待它完成。
  • @cppnoob 在计算机科学中没有“执行文件”这样的概念。要么这个文件被程序读取,要么这个文件是一个程序(可执行文件),因此可以运行。

标签: c++ if-statement while-loop process


【解决方案1】:

以下代码仅适用于 Windows。

bool EditFileExternallyBlocking(const TCHAR *FilePathName)
{
    SHELLEXECUTEINFO ShellExecuteInfo;

    ShellExecuteInfo.cbSize = sizeof(ShellExecuteInfo);
    ShellExecuteInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShellExecuteInfo.hwnd = NULL;
    ShellExecuteInfo.lpVerb = _T("edit");
    ShellExecuteInfo.lpFile = FilePathName;
    ShellExecuteInfo.lpParameters = NULL;
    ShellExecuteInfo.lpDirectory = NULL;
    ShellExecuteInfo.nShow = SW_SHOWDEFAULT;

    if (!ShellExecuteEx(&ShellExecuteInfo)) {
        return false;
    }

    if (ShellExecuteInfo.hProcess == NULL) {
        return false;
    }

    WaitForSingleObject(ShellExecuteInfo.hProcess, INFINITE);
    CloseHandle(ShellExecuteInfo.hProcess);
    return true;
}

bool EditFileExternallyPolling(const TCHAR *FilePathName, const std::function<void()> &Callback)
{
    SHELLEXECUTEINFO ShellExecuteInfo;

    ShellExecuteInfo.cbSize = sizeof(ShellExecuteInfo);
    ShellExecuteInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShellExecuteInfo.hwnd = NULL;
    ShellExecuteInfo.lpVerb = _T("edit");
    ShellExecuteInfo.lpFile = FilePathName;
    ShellExecuteInfo.lpParameters = NULL;
    ShellExecuteInfo.lpDirectory = NULL;
    ShellExecuteInfo.nShow = SW_SHOWDEFAULT;

    if (!ShellExecuteEx(&ShellExecuteInfo)) {
        return false;
    }

    if (ShellExecuteInfo.hProcess == NULL) {
        return false;
    }

    while (WaitForSingleObject(ShellExecuteInfo.hProcess, 0) == WAIT_TIMEOUT) {
        Callback();
    }

    CloseHandle(ShellExecuteInfo.hProcess);
    return true;
}

int main()
{
    std::cout << "Blocking way: \n";
    std::cout << std::boolalpha << EditFileExternallyBlocking(_T("G:\\test_text_file.txt")) << '\n';
    std::cout << "Process has stopped running\n\n";

    std::cout << "Polling way: \n";
    std::cout << std::boolalpha << EditFileExternallyPolling(_T("G:\\test_text_file.txt"),
        []() {
            std::cout << "Process is currently running...\n";
            Sleep(1000);
        }
    ) << '\n';
    std::cout << "Process has stopped running\n\n";

    return 0;
}

输出

Blocking way:
true
Process has stopped running

Polling way:
Process is currently running...
Process is currently running...
Process is currently running...
Process is currently running...
Process is currently running...
Process is currently running...
Process is currently running...
true
Process has stopped running

WaitForSingleObject 的其他可能返回值应在发布时处理。


参考文献

【讨论】:

    猜你喜欢
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多