如何从C程序中调用notepad.exe?

ask*_*k22 5 c windows file-io process

我在c写了一个时间表程序

#include<stdio.h> 
#include<conio.h> 
void main()
{
  int i=0;
  int selection;
  char day[20];
  char sub1[20];
  char sub2[20];
  char sub3[20];
  FILE *fp;
  fp=fopen("aa.txt","w");
  textcolor(5);
  textbackground(3);
  clrscr();
  while(i<3)
  {
    printf("Enter the day ");
    scanf("%s",day);
    printf("Enter the period 12.30-1:30 ");
    scanf("%s",sub1);
    printf("Enter the period 1.35-2.40 ");
    scanf("%s",sub2);
    printf("Enter the period 2.45-3.50 ");
    scanf("%s",sub3);
    fprintf(fp,"\n %s TIMETABLE IS AS FOLLOWS\n",day);
    fprintf(fp,"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
    fprintf(fp,"|~~~~~~~~~|~~~~~~~~~~~~~~~|~~~~~~~~~~~~~~|~~~~~~~~~~|\n");
    fprintf(fp,"| TIME    | 12.30-1.30    | 1.35-2.40    |2.45-3.50 |\n");
    fprintf(fp,"|~~~~~~~~~|~~~~~~~~~~~~~~~|~~~~~~~~~~~~~~|~~~~~~~~~~|\n");
    fprintf(fp,"| SUBJECT *     %s     * %s  * %s|\n",sub1,sub2,sub3);
    fprintf(fp,"|~~~~~~~~~|~~~~~~~~~~~~~~~|~~~~~~~~~~~~~~|~~~~~~~~~~|\n");
    i++;
  }
  printf(" Time table has been Created in the File aa.txt successfully");
  getch();
}
Run Code Online (Sandbox Code Playgroud)

当我完成时间表.时间表在a.txt文件中创建.我希望打开该文件并在记事本中自动显示.如何在c中编程呢?

Dan*_*ani 7

使用

system("notepad.exe aa.txt");
Run Code Online (Sandbox Code Playgroud)


Ant*_*tti 7

Dani已经描述了更简单的方法(使用system),因此我将使用Windows API描述另一种(更复杂但也更灵活)的方法.浏览API(概述 - >系统服务 - >进程和线程),有一个关于如何使用CreateProcess()函数创建进程的小例子.在你的情况下:

CreateProcess("notepad.exe",   // Name of program to execute
    "aa.txt",                  // Command line
    NULL,                      // Process handle not inheritable
    NULL,                      // Thread handle not inheritable
    FALSE,                     // Set handle inheritance to FALSE
    0,                         // No creation flags
    NULL,                      // Use parent's environment block
    NULL,                      // Use parent's starting directory 
    &si,                       // Pointer to STARTUPINFO structure
    &pi);                      // Pointer to PROCESS_INFORMATION structure
Run Code Online (Sandbox Code Playgroud)

然后等待Notepad进程退出,如示例中所述.

  • 请注意,虽然这可能是更复杂的方式,但它绝对是*推荐*在Windows上执行它的方式.除非你绝对*有*,否则不要使用`system`. (3认同)

Mat*_*lia 5

第三种方式:使用ShellExecuteshell函数告诉shell使用默认编辑器"只打开文件":

#include <windows.h>
#include <Shellapi.h>

// ...

if(ShellExecute(
    NULL,     // No parent window for error message boxes/...
    "open",   // Shell action ("verb") to be performed on the file (as opposed to "print", "explore", ...)
    "aa.txt", // File to be opened
    NULL,     // Command-line parameters - not used when opening documents
    NULL,     // Working directory - the current one is used by default
    SW_SHOW   // State of the window of the application being launched - SW_SHOW is the default
    )<=(HINSTANCE)32     // If ShellExecute returns a value <=32 it means that an error has occurred
   )
{
    puts("Cannot open aa.txt with the default editor - ShellExecute failed.");
}
Run Code Online (Sandbox Code Playgroud)

这将aa.txt使用txt文件的默认编辑器打开.

在我看来,这是最好的解决方案:

  • 它尊重用户对编辑器的选择(不像CreateProcess,只是打开notepad.exe); 如果我将PSPad设置为txt文件的默认编辑器,它将弹出PSPad而不是记事本.

  • 编辑器的搜索路径没有问题(在哪里notepad.exe?)

  • 它的行为是完全定义的,不像system函数,它依赖于command.com/ cmd.exe,它在Windows版本之间有细微的差别,并没有给你任何记录/简单的方法来检查操作是否成功;

  • 它不会给你带来任何"可移植性的错误感觉" system,它会很乐意在Linux机器上编译,但在运行时根本不能工作.