我正在尝试发送这个字符串:
服务器代码:
char bash[256] = "grep 'usernama' data/*.txt | awk -F':' '{print $1}' | uniq";
char result[1000] = system(bash);
send(connected, result,strlen(result), 0);
fflush(stdout);
错误信息:
error: array must be initialized with a brace-enclosed initializer
是否可以通过这种方式将grep结果发送给客户端?
最佳答案
system(3) 返回 fork(2) 的状态,但不是 fork 程序的标准输出。标准解决方案是使用管道:
char bash_cmd[256] = "grep 'usernama' data/*.txt | awk -F':' '{print $1}' | uniq":
char buffer[1000];
FILE *pipe;
int len;
pipe = popen(bash_cmd, "r");
if (pipe == NULL) {
perror("pipe");
exit(1);
}
fgets(buffer, sizeof(buffer), pipe);
len = strlen(bash_cmd);
bash_cmd[len-1] = '\0';
pclose(pipe);
关于c - 在套接字 C 中发送字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4283235/