问题在上面,我的Google搜索没有成功。我想我需要得到默认编辑器,然后使用system("editor file.txt");
?我怎么能得到默认的编辑器?C++(Unix):用默认编辑器打开一个文本文件
编辑:我不知道为什么,但stackoverflow不喜欢我的“嘿,”...然后不是。
问题在上面,我的Google搜索没有成功。我想我需要得到默认编辑器,然后使用system("editor file.txt");
?我怎么能得到默认的编辑器?C++(Unix):用默认编辑器打开一个文本文件
编辑:我不知道为什么,但stackoverflow不喜欢我的“嘿,”...然后不是。
没有官方的解决方案。这里是我打开一个文本编辑器推荐:
如果文件扩展名是.txt
和xdg-open
是avaliable对$PATH
和$DISPLAY
变量不为空,然后使用xdg-open
。否则使用/usr/bin/sensible-editor
(如果存在)。否则,请使用getenv("EDITOR")
,getenv("VISUAL")
或getenv("SELECTED_EDITOR")
。如果没有设置,请尝试nano
,nano-tiny
,然后vi
。
好的,谢谢。我现在在Windows上。我会在稍后尝试。 – Vider7CC
值得加入:'xdg-open'不是“C++的官方部分”,但它是“最广泛的?”的一部分吗?遵循Linux桌面标准:https://freedesktop.org/wiki/ –
有一个例子让缺省编辑环境,从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完整的代码。
可能出现[打开默认编辑器中bash文件]的副本(http://stackoverflow.com/questions/13627767/open-file-in-default-editor-from-bash)。 –
@FrédéricHamidi对不起,但我不能同意。这不是C++,有人可以给我一个替代系统()。 – Vider7CC
够公平的。我只是试图将你指向'xdg-open'。 –