使用C ++以编程方式打开文档

问题描述:

我有一个用C ++编写的控制台程序。现在我想打开一个手动文档(.txt或.pdf),每当程序的用户在控制台中键入手动。我如何做到这一点?

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

尝试编译此代码( Open.cpp )到 Open.exe
然后,您可以使用以下参数执行它:

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

C:\your file.doc

打开C:\your file.exe

Open "C:\your file.exe"

打开记事本

#include "windows.h"

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

程序说明:


  1. 您应该首先包含windows
    库( windows.h )以获取
    ShellExecute 是使用参数
    执行文件的函数 argv [1] > cb>
    另一个选项 lpOperation
    参数而不是open
    NULL。 explorefind都是
    也是选项,但他们不是
    打开文件

  2. SW_SHOWNORMAL 是到
    的常数显示正常模式下的程序(不是
    最小化或最大化) >
  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)