首页 > 解决方案 > 如何使用 while 循环将文件的内容读入两个单独的字符缓冲区?

问题描述

首先,我试图在 C 中模拟“uniq”Linux 命令,只使用系统调用。我目前正在尝试做的是将文本文件中的行读入两个不同的字符缓冲区,即char *buffer1char *buffer2.

这是我到目前为止所尝试的:

    char *buffer1 = malloc(MAX_LINE_LENGTH * sizeof(char));
    char *buffer2 = malloc(MAX_LINE_LENGTH * sizeof(char));

    // read the first line into buffer1 using read() sys call
    int i = 0;
    while (read(input_fd, &buffer1[i], 1) == 1) 
    {
        if (buffer1[i] == '\n')
        {
            buffer1[i] = '\0';
            write(output_fd, buffer1, i);
            break;
        } 
        else
        {
            i++;

            if (i > MAX_LINE_LENGTH)
            {
                perror("ERROR: Line is longer than the allocated buffer.\n");
                exit(EXIT_FAILURE);
            }
        }
    }

    // read the second line into buffer2
    int j = 0;
    char *temp_ptr;
    while(read(input_fd, &buffer2[j], 1) == 1)
    {
        if (buffer2[j] == '\n')
        {
            buffer2[j] = '\0';

            while (buffer2 != NULL)
            {
                if (strcmp(buffer1, buffer2) != 0)
                {
                    write(output_fd, buffer2, j);
                }

                j = 0;
               // after this if-statement, read in another line of text and compare it
               // to the string in buffer2 
                temp_ptr = buffer2;

                if (temp_ptr == buffer2) 
                {
                    temp_ptr = buffer1;
                }
                else
                {
                    temp_ptr = buffer2;
                }
            }
        
        }
        else
        {
            j++;

            if (j > MAX_LINE_LENGTH)
            {
                perror("ERROR: Line is longer than the allocated buffer.\n");
                exit(EXIT_FAILURE);
            }
        }
    }

这可以只使用一个while循环来完成吗?再一次,我想强调一个事实,我只能使用系统调用。任何帮助是极大的赞赏!

标签: csystem-callsuniq

解决方案


我想通了:

// read the first line into buffer1 using read() sys call
    int i = 0;
    
    while (read(input_fd, &buffer1[i], 1) != 0) 
    {
        if (buffer1[i] == '\n')
        {
            buffer1[i] = '\0';
            write(output_fd, buffer1, i);
            
            i = 0;
            break;
        } 
        else
        {
            i++;
            if (i > MAX_LINE_LENGTH)
            {
                perror("ERROR: Line is longer than the allocated buffer.\n");
                exit(EXIT_FAILURE);
            }
        }
    }

    int j = 0;
    ssize_t nr;
    while (read(input_fd, &buffer2[j], 1) != 0)
    {
        if (buffer2[j] == '\n')
        {
            buffer2[j] = '\0';
            if (strcmp(buffer1, buffer2) != 0)
            {
                write(output_fd, buffer2, j);
                if (nr == -1) {
                    perror("ERROR.\n");
                }
            }
            
            j = 0;
            char *current_ptr = buffer2;
            if (current_ptr == buffer2)
            {
                current_ptr = buffer1;
            } 
            else
            {
                current_ptr = buffer2;
            }
        }
        else
        {
            j++;
            if (j > MAX_LINE_LENGTH)
            {
                perror("ERROR: Line is longer than the allocated buffer.\n");
                exit(EXIT_FAILURE);
            }
        }
    }

推荐阅读