system(do_this); // which executes an external program to output a phrase on the screen
printf("=My taxes");
Run Code Online (Sandbox Code Playgroud)
输出:
$ 500
我的税
预期产量:
500 $ =我的税
我无法控制执行中do_this自动输出的代码'\n'.
这应该工作:
system("program | tr -d '\012\015'");
printf("=My taxes\n");
Run Code Online (Sandbox Code Playgroud)
其中,程序是要运行的程序.该tr命令从程序的输出中删除所有CR和LF字符(代码015和012八进制).该解决方案假定程序只输出一行(您的税),它适用于具有行结尾CR,LF,CR + LF,LF + CR(包括所有广泛使用的操作系统,如MacOS,Linux和Windows)的系统.