How can I call notepad.exe from a C program?
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?
Use
system("notepad.exe aa.txt");
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.
Third way: use the ShellExecute
shell function telling to the shell to "just open the file" with the default editor:
#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.
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.
精彩评论