我们这里说的是我的代码:
int main() {
char *number;
system("grep total myfile > filename");
printf(number);
}
Run Code Online (Sandbox Code Playgroud)
此代码从myfile中查找包含"total"的行,并将其输出到名为filename的新文件中.我试图将输出设置为char"number"直接,而不是必须写入/读取文件名.有没有办法做到这一点?
谢谢你的帮助!
如果我理解你的标题,你想output
从command
执行中得到,
#include <stdio.h>
#include <stdlib.h>
int main() {
char number[100];
FILE *f = popen("echo 200", "r");
while (fgets(number, 100, f) != NULL) {
printf( "%s\n", number );
}
pclose(f);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
或者你只是想将变量传递给命令并输出你的变量
#include <stdio.h>
#include <stdlib.h>
int main() {
int n = 100;
char buf[100];
sprintf(buf, "echo %d > filename", n); // format the command
system(buf); // execute
printf("%d\n", n); // print the variable
return 0;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4280 次 |
最近记录: |