2014-01-10 95 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完整的代码。

相关问题