Hi Folks,
Yes me again with another silly question although this time I hope its not as silly as before.
The below is fairly simple, I've written a small program (at the end of the thread), whose aim is to build a dos excution command which calls an external .exe program.
There is one argument being passed into Main. For my testing, the argument in argv[1] is:-
Code:
"C:\Documents and Settings\adminstrator\Mes documents\MiddleEarth\mepbm\Game63\turn0\test.pdf"
It's a pdf file and my program calls an external exe called pdftext.exe which converts the pdf file to a text file. I found the pdftext.exe on the web and it's in the same directory as argv[0].
So after extracting the various directory paths using simple subtr's and find functions, I call a function which returns a string that is the execution string. This is then called with the system call.
The execution string is as below:-
Code:
"C:\Documents and Settings\adminstrator\Mes documents\exec_str\test\Debug\pdftext.exe" -layout "C:\Documents and Settings\adminstrator\Mes documents\MiddleEarth\mepbm\Game63\turn0\test.pdf"
however, when I call this with:-
Code:
system(exe_str.c_str());
It fails, stating that "C:\Documents " is an unknown filename. I don't understand this as the first part of the execution string is double quotes to get around spaces in the directory name.
Even more strange, is if I cut and paste the execution string into the dos prompt, it works fine. Yet in my c++ program, it does not work.
Any ideas why ? I guess that I could change the directory name to replace the long-names with ~1 stuff but was hoping that I was doing something silly again.
Anyway, see below the full program.
Many thanks in advance.
Cheers
Starkhorn
Code:
#include <iostream>
#include <string>
using namespace std;
#define PDF_EXE "pdftext.exe"
string set_exec_str(string pdf_filename, string pdf_dir, string exe_dir);
int main(int argc, char *argv[])
{
string pdf_dir, exe_dir, pdf_filename, exe_str;
//assigns command line arguments to local variables. argv[1]
//is the pdf name with full directory path
exe_dir = argv[0];
pdf_dir = argv[1];
//gets the pdf name from pdf_dir.
pdf_filename = pdf_dir.substr(pdf_dir.find_last_of("\\")+1, pdf_dir.size());
//gets just the exe_dir by dropping the name of the c++ program
//just gets the directory where main.exe is stored
exe_dir = exe_dir.substr(0, exe_dir.find_last_of("\\"));
//gets just the pdf_dir without the name of the pdf file itself
pdf_dir = pdf_dir.substr(0, pdf_dir.find_last_of("\\"));
//calls function which builds the execution str
exe_str = set_exec_str(pdf_filename, pdf_dir, exe_dir);
system(exe_str.c_str());
return 1;
}
string set_exec_str(string pdf_filename, string pdf_dir, string exe_dir)
{
string str;
str += "\"";
str += exe_dir;
str += "\\";
str += PDF_EXE;
str += "\"";
str += " -layout";
str += " ";
str += "\"";
str += pdf_dir;
str += "\\";
str += pdf_filename;
str += "\"";
return str;
}