如何使用 while 循环将文件内容读入两个单独的字符缓冲区?

How to read contents of a file into two separate char buffers using a while loop?

首先,我尝试仅使用系统调用在 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] = '[=10=]';
            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] = '[=10=]';

            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 循环来完成吗?再一次,我想强调一个事实,我只能使用系统调用。非常感谢任何帮助!

我想通了:

// 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] = '[=10=]';
            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] = '[=10=]';
            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);
            }
        }
    }