Opening a document programmatically in C++
I have a console program written in C++. Now I want to open开发者_如何学Python 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
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
lpOperation
arguments 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)
Assuming you're on Windows, you're looking for the ShellExecute function. (Use the "open" verb)
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.
In Managed C++ is its very easy
System::Diagnostics::Process::Start(path);
done !
精彩评论