grep exec在c中的文件内容上

发布于 2025-02-01 16:13:29 字数 936 浏览 2 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场春暖 2025-02-08 16:13:29

execl的第一个参数必须是一个完整的路径。或者,您可以改用execlp

The first argument for execl must be a full path. Or you can use execlp instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文