Ok so I am using Microsoft Visual C++ 2008 express edition to compile my programs. I have used this for a while now and i want to make a program that will open another executable file. I have done this successfully with system(), however whenever I put spaces in the file path, it gives me an error

Eventually I will want to have the program execute the file and then take a string that this program produces and put that in a variable. I have searched around online and i believe that the system() command is not what i need for this job (would win32 be better to use?). What can i use and what header files does it need? (Also if the program can be run without showing that would be great, so it doesn't bother the user as much)

Thanks in advance.

Recommended Answers

All 3 Replies

You can still use system() if you want. All it does is pass the string to the command shell, so the string has to look like it would in the command prompt. For names with spaces, wrap the path in double quotations:

#include <iostream>
#include <string>
#include <cstdlib>

int main()
{
    using namespace std;

    string cmd = "type ";
    string filename;

    cout << "Type a filename: ";
    if (getline(cin, filename))
    {
        system((cmd + "\"" + filename + "\"").c_str());
    }
}

Welcome mrfred,
I think you are looking for this - Creating a Child Process with Redirected Input and Output

SUMMARY:MSDN topic demonstrates how to create a child process using the CreateProcess function from a console process. It also demonstrates a technique for using anonymous pipes to redirect the child process's standard input and output handles.

Thanks for the info. However, I am a little rusty in C++, not having done it for a while. Could you help me refresh my memory as to how to use createprocess, the sit you gave was a little confusing. thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.