2014-01-10 96 views
5

問題在上面,我的Google搜索沒有成功。我想我需要得到默認編輯器,然後使用system("editor file.txt");?我怎麼能得到默認的編輯器?C++(Unix):用默認編輯器打開一個文本文件

編輯:我不知道爲什麼,但stackoverflow不喜歡我的「嘿,」...然後不是。

+1

可能出現[打開默認編輯器中bash文件]的副本(http://stackoverflow.com/questions/13627767/open-file-in-default-editor-from-bash)。 –

+0

@FrédéricHamidi對不起,但我不能同意。這不是C++,有人可以給我一個替代系統()。 – Vider7CC

+0

夠公平的。我只是試圖將你指向'xdg-open'。 –

回答

7

沒有官方的解決方案。這裏是我打開一個文本編輯器推薦:

如果文件擴展名是.txtxdg-open是avaliable對$PATH$DISPLAY變量不爲空,然後使用xdg-open。否則使用/usr/bin/sensible-editor(如果存在)。否則,請使用getenv("EDITOR"),getenv("VISUAL")getenv("SELECTED_EDITOR")。如果沒有設置,請嘗試nano,nano-tiny,然後vi

+0

好的,謝謝。我現在在Windows上。我會在稍後嘗試。 – Vider7CC

+1

值得加入:'xdg-open'不是「C++的官方部分」,但它是「最廣泛的?」的一部分嗎?遵循Linux桌面標準:https://freedesktop.org/wiki/ –

3

有一個例子讓缺省編輯環境,從visudo命令(它使用默認編輯器打開sudoers文件)源

/* 
    * Check EDITOR and VISUAL environment variables to see which editor 
    * the user wants to use (we may not end up using it though). 
    * If the path is not fully-qualified, make it so and check that 
    * the specified executable actually exists. 
    */ 
    if ((UserEditor = getenv("EDITOR")) == NULL || *UserEditor == '\0') 
    UserEditor = getenv("VISUAL"); 
    if (UserEditor && *UserEditor == '\0') 
    UserEditor = NULL; 
    else if (UserEditor) { 
    if (find_path(UserEditor, &Editor, getenv("PATH")) == FOUND) { 
     UserEditor = Editor; 
    } else { 
     if (def_flag(I_ENV_EDITOR)) { 
     /* If we are honoring $EDITOR this is a fatal error. */ 
     (void) fprintf(stderr, 
      "%s: specified editor (%s) doesn't exist!\n", 
      Argv[0], UserEditor); 
     Exit(-1); 
     } else { 
     /* Otherwise, just ignore $EDITOR. */ 
     UserEditor = NULL; 
     } 
    } 
    } 

您可以檢查http://www.opensource.apple.com/source/sudo/sudo-9/sudo/visudo.c完整的代碼。

相關問題