grep exec在c中的文件内容上
我正在尝试使用Excel将GREP应用于文件内容。但是,它不起作用。我被困在它上。我不知道如何在EXEC调用中获取文件的内容。
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main() {
int link[2];
pid_t pid;
char foo[4096];
if (pipe(link)==-1)
die("pipe");
if ((pid = fork()) == -1)
die("fork");
if(pid == 0) {
const char *input = "output1.txt";
int fd = open(input, O_RDONLY);
dup2(fd, 0);
execl("grep", "grep" ,"com", NULL);
close(fd);
exit(1);
} else {
}
return 0;
}
输出1.txt如下所示:
c1.txt
c1.txt~
commands1
commands1~
commandSample1.txt
commandSample1.txt~
commandSample2.txt
foo.txt
output1.txt
output2.txt
text.txt
text.txt~
建议很棒。
I am trying to apply the grep on file content using excel. But, it is not working. I am stuck on it. I don't know how to get the content of the file in the exec call.
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main() {
int link[2];
pid_t pid;
char foo[4096];
if (pipe(link)==-1)
die("pipe");
if ((pid = fork()) == -1)
die("fork");
if(pid == 0) {
const char *input = "output1.txt";
int fd = open(input, O_RDONLY);
dup2(fd, 0);
execl("grep", "grep" ,"com", NULL);
close(fd);
exit(1);
} else {
}
return 0;
}
The output1.txt is shown below:
c1.txt
c1.txt~
commands1
commands1~
commandSample1.txt
commandSample1.txt~
commandSample2.txt
foo.txt
output1.txt
output2.txt
text.txt
text.txt~
Suggestions would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
execl
的第一个参数必须是一个完整的路径。或者,您可以改用execlp
。The first argument for
execl
must be a full path. Or you can useexeclp
instead.