如何从 C 程序调用 notepad.exe?
我用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();
}
当我完成时间表时, 。时间表在.txt 文件中创建。我希望打开该文件并在记事本中自动显示。如何用c语言编程?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
Use
Dani 已经描述了更简单的方法(使用
system
),所以我将使用 Windows API。浏览 API(概述 -> 系统服务 -> 进程和线程),有一个关于如何使用 CreateProcess() 函数创建进程。对于您的情况:然后等待记事本进程退出,如示例中所述。
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:And then wait for the Notepad process to exit, as described in the example.
第三种方式:使用
ShellExecute
shell 函数告诉 shell 使用默认编辑器“仅打开文件”:
这将使用
txt
的默认编辑器打开aa.txt
文件。在我看来,这是最好的解决方案:
它尊重用户对编辑器的选择(与
CreateProcess
不同,它只是打开notepad.exe
);如果我将 PSPad 设置为 txt 文件的默认编辑器,它将弹出 PSPad 而不是记事本。编辑器的搜索路径没有问题(
notepad.exe
在哪里?)它的行为是完全定义的,与
system
函数不同,它依赖于command.com
/cmd .exe
,Windows 版本之间存在细微差别,并且不提供您可以使用任何有记录的/简单的方法来检查操作是否成功;它不会给你任何像
系统
那样的“错误的可移植性感觉”,它可以在Linux机器上愉快地编译,但在运行时根本无法工作。Third way: use the
ShellExecute
shell function telling to the shell to "just open the file" with the default editor:This will open
aa.txt
with the default editor fortxt
files.In my opinion, this is the best solution:
it respects the user's choice for the editor (unlike
CreateProcess
, which just opensnotepad.exe
); if I set PSPad as the default editor for txt files, it will pop up PSPad and not notepad.it doesn't have problems with search paths for the editor (where is
notepad.exe
?)its behavior is fully defined, unlike the
system
function, which relies oncommand.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;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.