system(do_this); // which executes an external program to output a phrase on the screen
printf("=My taxes");
輸出:從程序中刪除 ' N'
$ 500
=我的稅
預期輸出:
$ 500 =我的稅款
我無法控制在do_this
中執行的代碼,該代碼會自動輸出'\n'
。
system(do_this); // which executes an external program to output a phrase on the screen
printf("=My taxes");
輸出:從程序中刪除 ' N'
$ 500
=我的稅
預期輸出:
$ 500 =我的稅款
我無法控制在do_this
中執行的代碼,該代碼會自動輸出'\n'
。
這應該工作:
system("program | tr -d '\012\015'");
printf("=My taxes\n");
其中程序是您想要運行的程序。 tr
命令從程序的輸出中刪除所有CR和LF字符(代碼015和012八進制)。解決方案假設程序只輸出一行(您的稅),它適用於行結束符爲CR,LF,CR + LF,LF + CR(包括所有廣泛使用的操作系統,如MacOS,Linux和Windows)的系統。
另一種選擇是改變你的系統調用利用utils的的剝離換行符
相反,你的系統調用是
system("a.out");
改變它
system("a.out | awk '{ printf(\"%s\",$0); }'");
沒有標準的方式,沒有。也許有一些特定的控制檯功能,但這些不是C或其標準庫的一部分。 –
不要使用'system()',而是使用'popen()'或類似的東西,以便捕獲輸出並處理它。 –