我怎么叫记事本?来自C程序的exe ?

时间:2021-06-11 16:03:52

i have written a time table program in c

我用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();
}

when i finish the timetable . the time table is created in a.txt file. i want that file to be opened and show me automatically in a notepad. how to program that in c?

当我完成时间表的时候。时间表是在a中创建的。txt文件。我希望打开那个文件,并自动显示在记事本中。如何在c语言中编程?

3 个解决方案

#1


7  

Use

使用

system("notepad.exe aa.txt");

#2


7  

Dani already described the easier way (using system), so I'll just describe the other (more complicated but also more flexible) way to do it using the Windows API. Browsing the API (Overview -> System Services -> Processes and Threads), there's a small example on how to create a process using the CreateProcess() function. In your case:

Dani已经描述了更简单的方法(使用系统),所以我将用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

And then wait for the Notepad process to exit, as described in the example.

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

#3


5  

Third way: use the ShellExecute shell function telling to the shell to "just open the file" with the default editor:

第三种方法:使用ShellExecute shell函数告诉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.");
}

This will open aa.txt with the default editor for txt files.

这将打开aa。txt与txt文件的默认编辑器。

In my opinion, this is the best solution:

在我看来,这是最好的解决办法:

  • it respects the user's choice for the editor (unlike CreateProcess, which just opens notepad.exe); if I set PSPad as the default editor for txt files, it will pop up PSPad and not notepad.

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

  • it doesn't have problems with search paths for the editor (where is notepad.exe?)

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

  • its behavior is fully defined, unlike the system function, which relies on command.com/cmd.exe, which have subtle differences between Windows versions and don't give you any documented/easy way to check if the operation succeeded;

    它的行为是完全定义的,不像系统功能,它依赖于command.com/cmd.exe,它在Windows版本之间有细微的差别,并且没有提供任何文档化/简单的方法来检查操作是否成功;

  • it doesn't give you any "false feeling of portability" like the system, that will happily compile on a Linux machine but will simply not work at runtime.

    它不会像系统那样给你带来任何“可移植性的错误感觉”,它会在Linux机器上很好地编译,但是在运行时不会运行。

#1


7  

Use

使用

system("notepad.exe aa.txt");

#2


7  

Dani already described the easier way (using system), so I'll just describe the other (more complicated but also more flexible) way to do it using the Windows API. Browsing the API (Overview -> System Services -> Processes and Threads), there's a small example on how to create a process using the CreateProcess() function. In your case:

Dani已经描述了更简单的方法(使用系统),所以我将用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

And then wait for the Notepad process to exit, as described in the example.

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

#3


5  

Third way: use the ShellExecute shell function telling to the shell to "just open the file" with the default editor:

第三种方法:使用ShellExecute shell函数告诉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.");
}

This will open aa.txt with the default editor for txt files.

这将打开aa。txt与txt文件的默认编辑器。

In my opinion, this is the best solution:

在我看来,这是最好的解决办法:

  • it respects the user's choice for the editor (unlike CreateProcess, which just opens notepad.exe); if I set PSPad as the default editor for txt files, it will pop up PSPad and not notepad.

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

  • it doesn't have problems with search paths for the editor (where is notepad.exe?)

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

  • its behavior is fully defined, unlike the system function, which relies on command.com/cmd.exe, which have subtle differences between Windows versions and don't give you any documented/easy way to check if the operation succeeded;

    它的行为是完全定义的,不像系统功能,它依赖于command.com/cmd.exe,它在Windows版本之间有细微的差别,并且没有提供任何文档化/简单的方法来检查操作是否成功;

  • it doesn't give you any "false feeling of portability" like the system, that will happily compile on a Linux machine but will simply not work at runtime.

    它不会像系统那样给你带来任何“可移植性的错误感觉”,它会在Linux机器上很好地编译,但是在运行时不会运行。