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

如何从C程序调用notepad.exe?,c,windows,file-io,process,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); 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();
}
#包括
#包括
void main()
{
int i=0;
int选择;
查日[20];
char sub1[20];
char-sub2[20];
char-sub3[20];
文件*fp;
fp=fopen(“aa.txt”,“w”);
textcolor(5);
文本背景(3);
clrsc();
而(i使用


Dani已经描述了更简单的方法(使用
system
),因此我将只描述使用的另一种(更复杂但也更灵活)方法。浏览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
然后等待记事本进程退出,如示例中所述。

第三种方式:使用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.");
}
#包括
#包括
// ...
如果(shell)执行(
NULL,//没有用于错误消息框的父窗口/。。。
“open”//要对文件执行的Shell操作(“动词”)(与“print”、“explore”相对)
“aa.txt”,//要打开的文件
NULL,//命令行参数-打开文档时不使用
NULL,//工作目录-默认使用当前目录
SW_SHOW//正在启动的应用程序窗口的状态-默认为SW_SHOW

)打开另一个应用程序是特定于平台的。通常,您会为此调用操作系统API。在标准C中,无法做到这一点,您必须求助于特定于平台的API(或利用
系统
函数的特定于平台的行为)。你在哪个平台上工作?@Dani记事本可以是任何记事本++、UltraEdit、gedit、mcedit…记得在文件指针上调用
fclose()
。它是
int main(void)
,而不是
void main()
。我建议使用
%windir%/notepad.exe
作为记事本。exe
是首选。@Dani(“notepad.exe aa.txt”)这对我不起作用。有什么原因吗?@mbx:绝对不要这样做。记事本并不总是在Windows目录中。关于不使用硬编码路径,您的想法是正确的,但在这种情况下,最好在路径中打开记事本。@ask22:如果应用程序
Notepad.exe
不在您的pa中,它对您不起作用有一种简单的测试方法可以隔离出代码特有的任何问题。从“开始”菜单打开“运行”对话框,然后键入
notepad.exe aa.txt
。如果有效,则代码就是问题所在。如果无效,则系统配置不同,代码可能没有问题。@Cody Gray“notepad.exe aa.txt“在运行对话框中,它会提示我“是否创建aa.txt文件”?请注意,虽然这可能是更复杂的方法,但它绝对是在Windows上执行此操作的推荐方法。除非您必须使用,否则不要使用
system
。很好。尽管在我的情况下(C++/CX)第一个参数必须是notepad.exe的完整路径,第二个参数必须有完整的命令行,其中不仅包括参数,还包括可执行文件。
#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.");
}