Tell me, please. I need to create 2 exe-files: test1.exe and test2.exe. We put in different folders, let the first place in c:\home\test1.exe and the second in c:\work\test2.exe . write two text files:

 test1.txt: c:\home\test1.exe c:\work\test2.exe test2.txt: c:\work\test2.exe c:\home\test1.exe 

then we write the program prog.exe which takes as an argument the path to one of two text files. The results of the work are:

 prog.exe test1.txt: запуск сначала test1, затем test2 prog.exe test2.txt: запуск сначала test2, затем test1 

The question is how to run .exe files from a read text file?

 #include <iostream> #include <fstream> #include <process.h> using namespace std; int main(int argc, char *argv[]) { cout << "argc = " << argc << endl; for (int i = 0; i < argc; i++) { cout << "Argument: " << i << " = " << argv[i] << endl; } if (argc != 2) { cout << "Error" << endl; exit(-1); } char ch; ifstream infile; infile.open(argv[1]); if (!infile) { cout << "errrrror: cant open a file" << argv[1]; exit(-1); } while (infile) { infile.get(ch); cout << ch; } cout << endl; system("pause"); return 0; } 

enter image description here

    2 answers 2

    The easiest is the system .

    More difficult - spawn...

    Even harder is to use a Windows API, such as CreateProcess .

    If I correctly understood the question asked, of course ...

    PS More difficult at the same time means "but more opportunities" :)

    • Tell me, please, how to use the system correctly? - Nikita Gusev
    • He needs to pass a string, as if you typed it in the system prompt ... - Harry
    • @NikitaGusev something like this: system("run_file.exe") - pavel
    • @Harry and what is bad system? What you ranked a simple solution is a difficult one. - pavel
    • @pavel Well, because this creates an extra shell session - does it need it? for example, it will not be very good if a GUI application is launched from there. Yes, and there are almost no management capabilities, as well as checks, whether it was launched or not ... - Harry

    If you don’t disgust you do n’t need to crossplate, you can use the .Net class Process

     #using <System.dll> using namespace System; using namespace System::Diagnostics; Process^ myProcess = gcnew Process(); myProcess->StartInfo->FileName = "C:\\Program Files\\LibreOffice 5\\program\\soffice.exe"; myProcess->Start(); 

    You can also try ShellExecute

     #include <Windows.h> LPCTSTR path = L"C:\\Program Files\\LibreOffice 5\\program\\soffice.exe"; ShellExecute(NULL, L"open", path, NULL, NULL, SW_SHOWDEFAULT); 
    • one
      Big mistake to talk about plus C + / CLI :) - isnullxbh