2016-07-30 185 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; 
}