Rafo 554
Rafo 554

Reputation: 109

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

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());

Upvotes: 3

Views: 343

Answers (2)

HAL9000
HAL9000

Reputation: 2188

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.

Upvotes: 1

GirkovArpa
GirkovArpa

Reputation: 4912

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.

Upvotes: 2

Related Questions