2011-08-14 35 views
5

我在C如何從C程序調用notepad.exe?

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

寫了一個時間表程序時,我完成的時間表。時間表在a.txt文件中創建。我希望打開該文件並自動顯示在記事本中。如何在c中編程?

+1

打開另一個應用程序是平臺特定的。通常你會爲此操作一個OS API調用 – mbx

+3

在標準C中沒有辦法做到這一點,你必須求助於一些特定於平臺的API(或利用'system'函數的平臺特定行爲)。你在哪個平臺上工作? –

+0

@mbx如何找到要使用的API調用。我正在使用windows xp – ask22

回答

7

使用

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

它不適用於我 – ask22

+0

我建議使用'%windir%/ notepad.exe'作爲notepad.exe'''將是首選。 – mbx

+0

@mbx它也不起作用 – ask22

7

丹妮已經描述(使用system)的更簡單的方法,所以我就描述了其它(更復雜,也更靈活)的方式使用Windows API做到這一點。瀏覽API(概述 - >系統服務 - >進程和線程),關於如何使用CreateProcess()函數create a process有一個小例子。在你的情況下:

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 

然後等待記事本進程退出,如示例中所述。

+3

請注意,雖然這可能是更復雜的方式,但它絕對是Windows推薦的方式。除非你絕對擁有*,否則不要使用'system'。 –

+0

太好了。雖然在我的情況下(C++/CX),第一個參數必須是notepad.exe的完整路徑,第二個參數必須有完整的命令行,其中不僅包括參數,還包括可執行文件。 –

5

第三條道路:用ShellExecute外殼功能告訴給外殼的「剛打開文件」使用默認編輯:

#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."); 
} 

這將打開aa.txttxt文件的默認編輯器。

在我看來,這是最好的解決辦法:

  • 它尊重用戶的選擇爲編輯器(不像CreateProcess,這只是打開notepad.exe);如果我將PSPad設置爲txt文件的默認編輯器,它將彈出PSPad而不是記事本。

  • 它沒有與搜索路徑問題的編輯器(在哪裏notepad.exe?)

  • 其行爲完全定義,不像system功能,它依賴於command.com/cmd.exe,其中有細微的差別在Windows版本之間,不要給你任何記錄/簡單的方法來檢查操作是否成功;

  • 它不會給你任何像「system」那樣的「可移植性錯誤感覺」,它可以在Linux機器上愉快地編譯,但在運行時將不起作用。