我怎样才能从C程序调用notepad.exe? [英] How can I call notepad.exe from a C program?

查看:183
本文介绍了我怎样才能从C程序调用notepad.exe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个时间表程序c

pre $ #include< stdio.h>
#include< conio.h>
void main()
{
int i = 0;
int选择;
char char [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(输入句点12.30-1:30);
scanf(%s,sub1);
printf(输入期间1.35-2.40);
scanf(%s,sub2);
printf(输入期间2.45-3.50);
scanf(%s,sub3);
fprintf(fp,\ n%s TIMETABLE is as FOLLOWS \\\
,day);
fprintf(fp,〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜 ~~~~~~~~~~~~ \\\
);
fprintf(fp,| ~~~~~~~~~ | | ~~~~~~~~~~~~~ | ~~~~~~~~~~ | \\\
);
fprintf(fp,| TIME | 12.30-1.30 | 1.35-2.40 | 2.45-3.50 | \\\
);
fprintf(fp,| ~~~~~~~~~ | | ~~~~~~~~~~~~~ | ~~~~~~~~~~ | \\\
);
fprintf(fp,| SUBJECT *%s *%s *%s | \\\
,sub1,sub2,sub3);
fprintf(fp,| ~~~~~~~~~ | | ~~~~~~~~~~~~~ | ~~~~~~~~~~ | \\\
);
i ++;

printf(时间表已成功创建在文件aa.txt中);
getch();
}

当我完成时间表。时间表在a.txt文件中创建。我想要打开这个文件并自动显示在记事本中。如何在c中编程?

解决方案

Dani已经描述了更简单的方法(使用 system ),所以我只会用 Windows API 。浏览API(概述 - >系统服务 - >进程和线程),有一个关于如何使用CreateProcess()函数创建一个进程。在你的情况:

pre $ code CreateProcess(notepad.exe,//执行
的程序名称aa。 txt,//命令行
NULL,//进程句柄不可继承
NULL,//线程句柄不可继承
FALSE,//设置句柄继承为FALSE
0, //没有创建标志
NULL,//使用父节点的环境块
NULL,//使用父节点的起始目录
& si,//指向STARTUPINFO结构的指针
& ); //指向PROCESS_INFORMATION结构的指针

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


i have written a time table program in 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?

解决方案

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:

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.

这篇关于我怎样才能从C程序调用notepad.exe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆