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
2010-04-30 15:56:58
+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 :
- You should first include windows library (windows.h) to get ShellExecute and GetDesktopWindow function.
- ShellExecute is the function to execute the file with parameter argv[1] that is path to the file to be opened
- Another option for
lpOperationarguments instead of"open"is NULL."explore"and"find"are also the options but they are not for opening a file. - SW_SHOWNORMAL is the constant to show the program in normal mode (not minimize or maximize)
Zai
2010-04-30 20:20:20
Wouldn't using `start` be the same as your `open`?
Blindy
2010-04-30 20:28:53
Yes, it's the same, but we want to open a program/file without `dependency` for using `start` command
Zai
2010-04-30 20:49:48
+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
2010-04-30 20:27:39
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
2010-05-01 01:10:10