tags:

views:

72

answers:

3

I have a console program written in C++. Now I want to open a manual document(in .txt or .pdf) everytime a user of the program types 'manual' in console. How can I do this? Any links to a tutorial would be helpful.Thanks

+1  A: 

Assuming you're on Windows, you're looking for the ShellExecute function. (Use the "open" verb)

Billy ONeal
+3  A: 

Try to compile this code (Open.cpp) to Open.exe Then, you can execute it with (for example) these parameters :

Open "C:\your file.doc"

Open "C:\your file.exe"

Open notepad

#include "windows.h"

int main(int argc, char *argv[])
{
    ShellExecute(GetDesktopWindow(), "open", argv[1], NULL, NULL, SW_SHOWNORMAL);
}

Explanation of the program :

  1. You should first include windows library (windows.h) to get ShellExecute and GetDesktopWindow function.
  2. ShellExecute is the function to execute the file with parameter argv[1] that is path to the file to be opened
  3. Another option for lpOperation arguments instead of "open" is NULL. "explore" and "find" are also the options but they are not for opening a file.
  4. SW_SHOWNORMAL is the constant to show the program in normal mode (not minimize or maximize)
Zai
Wouldn't using `start` be the same as your `open`?
Blindy
Yes, it's the same, but we want to open a program/file without `dependency` for using `start` command
Zai
+1  A: 

In standard, platform independent, C and C++ you can use the system function to pass the name of an application to open your files.

For example, using Windows:

const char text_filename[] = "example.txt";
const char text_application[] = "notepad.exe";

std::string system_str;
system_str = text_application;
system_str += " ";
system_str += text_filename;

// Execute the application
system(system_str.c_str());

The text you send to the system function is platform specific.

Thomas Matthews
Thanks, it works! It fails when I try to open a pdf file using adobe. It says "acrord32.exe is not recognized as an internal or external command, operable or program batch file".
James