Can you help me? I need to run a file type of .exe
. I have got two test1.txt
and test2.txt
files and in those files there are two PATHes to pr1.exe and pr2.exe . But in the first txt file I run pr1.exe and pr2.exe , in another one there are pr2.exe and pr1.exe .
#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;
}
For example: I write path of the test1.txt
and it prints two PATHes of pr1.exe and pr2.exe it's like:
"C:\\Users\\N\\Desktop\\process\\The 2d file\\Debug\\The 2d file.exe"
"C:\\Users\\N\\Desktop\\process\\the 1st file\\Debug\\the 1st file.exe"
Can you advice me how I should run them?
What you're looking for is the CreateProcess
function, more info can be found on msdn :
// start the program up
#include "windows.h"
std::wstring("pr1.exe");
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if(!CreateProcess(NULL, (LPWSTR)wstr.c_str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
//error
}
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
Note: If you're not sending any command line paramaters with your program, you could also swap the first and second argument.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.