使用记事本C++打开 txt 文件时无需等待

Not waiting when open a txt file with notepad C++

本文关键字:等待 文件 txt 记事本 C++ 打开      更新时间:2023-10-16

当我在C++中使用记事本打开.txt文件时,我的程序在关闭记事本之前不会继续。

如何让我的代码继续而不是像这样等待?

法典:

string a;
string topicName;
a = "C:\Hello\Hi\Hi.txt";
topicName = "notepad "" + a + """;
system(topicName.c_str());

使用 popen 而不是system

#include <iostream>
int main() {
popen("notepad.exe", "r");
std::cout << "I'm still running!" << std::endl;
}

我在 Windows 10 64 位上用g++编译了它,它可以工作。

如果你愿意使用system函数,你可以用startshell 函数打开记事本:

system("start notepad c:\my\path\hello.txt");

startshell 函数在后台运行。它就像在屏幕上"点击"了什么东西一样。因此,如果您想在默认编辑器而不是记事本中打开hello.txt。您还可以执行以下操作:

system("start c:\my\path\hello.txt");

当然,这是特定于Windows的,并且许多人不建议在生产代码中使用system