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

Viewed 162

When I open a .txt file with Notepad in C++, my program does not continue until I close Notepad.

How can I have my code continue instead of waiting like this?

Code:

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

Use popen instead of system.

#include <iostream>

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

I compiled this with g++ on Windows 10 64-bit and it works.

If you are willing to use the system function, you can open notepad with the start shell function:

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

The start shell function runs things in the background. It acts as if something was "clicked" on the screen. So if you want to open hello.txt in the default editor rather than notepad. You can also do:

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

Naturally this is windows-specific, and using system is by many not recommended for production code.

Related