2016-07-30 187 views
1

我想编写一个C程序,它接收Emacs缓冲区的一个区域并用它的输出替换该区域。将Emacs缓冲区传递给C

这里是我的C程序:

#include <stdio.h> 

int main(int argc, char *argv[]) { 
    printf("The argument given was: %s\n",argv[1]); 
} 

g++ -Wall -o c_example c_example.c 

编译这一点,并把二进制在我的道路。当我在终端做

c_example Hello 

,我得到

The argument given was: Hello 

,但如果我在Emacs的缓冲区选择“你好”和使用shell命令,对区域与“铜M- | c_example “将其替换为

The argument given was: (null) 

改为。为什么是这样?

+2

不是一个Emacs用户,但我想这竖线表示您管道缓冲作为标准输入。 – a3f

回答

4

传递给filter命令的emacs缓冲区的内容不是从命令行中检索的,而是从标准输入中检索的。您应该使用fgets()<stdio.h>中的任何其他输入函数来读取它。

试试这个版本:

#include <stdio.h> 

int main(void) { 
    char line[80]; 
    if (fgets(line, sizeof line, stdin)) { 
     printf("The first line of the buffer is: %s", line); 
    } 
    return 0; 
}