使用 UNIX 的 read() 系统调用来查找用户给定的模式

发布于 2024-11-29 06:58:53 字数 919 浏览 0 评论 0原文

我正在尝试使用 C 程序模拟 UNIX 的 grep pattern(仅供学习)。我编写的代码给了我一个运行时错误。

#include <fcntl.h>
#include <stdio.h>
#include <string.h>

#define MAXLENGTH 1000
char userBuf[MAXLENGTH];

int main ( int argc, char *argv[])
{
        int numOfBytes,fd,i;


        if (argc != 2)
                printf("Supply correct number of arguments.\n");
                //exit(1);

        fd =open("pattern.txt",O_RDWR);

        if ( fd == -1 )
                printf("File does not exist.\n");
                //exit(1);

        while ( (numOfBytes = read(fd,userBuf,MAXLENGTH)) > 0 )
                ;

        printf("NumOfBytes = %d\n",numOfBytes);

        for(i=0;userBuf[i] != '\0'; ++i)
        {
                if ( strstr(userBuf,argv[1]) )
                        printf("%s\n",userBuf);
        }

}

程序无限打印包含 pattern 的行。我尝试调试,但无法找出错误。请告诉我哪里错了,

谢谢

I am trying to emulate grep pattern of UNIX using a C program( just for learning ). The code that i have written is giving me a run time error..

#include <fcntl.h>
#include <stdio.h>
#include <string.h>

#define MAXLENGTH 1000
char userBuf[MAXLENGTH];

int main ( int argc, char *argv[])
{
        int numOfBytes,fd,i;


        if (argc != 2)
                printf("Supply correct number of arguments.\n");
                //exit(1);

        fd =open("pattern.txt",O_RDWR);

        if ( fd == -1 )
                printf("File does not exist.\n");
                //exit(1);

        while ( (numOfBytes = read(fd,userBuf,MAXLENGTH)) > 0 )
                ;

        printf("NumOfBytes = %d\n",numOfBytes);

        for(i=0;userBuf[i] != '\0'; ++i)
        {
                if ( strstr(userBuf,argv[1]) )
                        printf("%s\n",userBuf);
        }

}

The program is printing infinitely, the lines containing the pattern . I tried debugging , but couldn't figure out the error. Please let me know where am i wrong.,

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

独行侠 2024-12-06 06:58:53

假设字符串是“fooPATTERN”。第一次执行循环时,您检查“fooPATTERN”中的模式并找到它。然后第二次循环时,检查“ooPATTERN”中的模式并再次找到它。然后第三次,您检查“oPATTERN”中的模式并再次找到它。

既然你这样做是为了学习,我就不告诉你更多了。您可以决定如何最好地解决它。至少有两种根本不同的方法可以解决这个问题。一是在循环的每次传递中减少执行次数,以确保只找到一次。另一个是确保循环的下一次通过已找到的任何模式。

需要考虑的一件事:如果模式是“oo”并且字符串是“ooo”,那么应该找到多少个模式? 1还是2?

Say the string is "fooPATTERN". Your first time through the loop, you check for the pattern in "fooPATTERN" and find it. Then your second time through the loop, you check for the pattern in "ooPATTERN" and find it again. Then your third time, you check for the pattern in "oPATTERN" and find it again.

Since you're doing this to learn, I won't tell you much more. You can decide how best to solve it. There are at least two fundamentally different ways you could solve it. One is to do less on each pass of the loop to ensure you only find it once. The other is to make sure your next pass of the loop is past any pattern that was found.

One thing to think about: If the pattern is 'oo' and the string is 'ooo', how many patterns should be found? 1 or 2?

姐不稀罕 2024-12-06 06:58:53
  1. “读取”不会用空字符分隔数据。
  2. while 循环应该包含 for 循环 - 但事实并非如此
  1. The 'read' does not delimit the data with a null character.
  2. The while loop should encompase the for loop - it doesn't
〗斷ホ乔殘χμё〖 2024-12-06 06:58:53

首先,如果您刚刚学习 C,则不应使用带有 openread 的原始 Unix i/o。从带有 的标准 C i/o 开始fopenfread/fscanf/fgets 等等。

其次,您将文件的连续部分读入同一个缓冲区,每次都覆盖缓冲区,并且只处理缓冲区的最后内容。

第三,当您使用read()读取缓冲区时,没有任何东西可以保证缓冲区将以零终止。事实上,通常不会。

第四,您没有在循环体中使用 i 变量。我无法准确说出您在这里拍摄的目的,但对相同的数据执行相同的操作无数次肯定不是这样。

第五,始终使用您可以遵守的最完整的警告设置进行编译 - 至少使用 GCC 的 -Wall 。它应该抱怨您调用 read() 而不包含

First, you shouldn't be using raw Unix i/o with open and read if you're just learning C. Start with standard C i/o with fopen and fread/fscanf/fgets and so forth.

Second, you're reading in successive pieces of the file into the same buffer, overwriting the buffer each time, and only ever processing the last contents of the buffer.

Third, nothing guarantees that your buffer will be zero-terminated when you read into it with read(). In fact, it usually won't be.

Fourth, you're not using the i variable in the body of your loop. I can't tell exactly what you were shooting for here, but doing the same thing on the same data umpteen thousand times surely wasn't it.

Fifth, always compile with the fullest warning settings you can abide -- at lest -Wall with GCC. It should have complained that you call read() without including <unistd.h>.

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