如何将 .txt 文件中的行读入两个单独的缓冲区?

How can I read lines from a .txt file into two separate buffers?

我有两个缓冲区 char *buffer1char *buffer2 分配在堆上,使用 malloc。从文本文件中,我想将一行读入 buffer1,将第二行读入 buffer2,如何在同一个 while 循环下完成此操作?

到目前为止,我已经弄清楚如何将第一行文本读入 buffer1:

int i = 0;
while (read(input_fd, &buffer1[i], 1) == 1) {
    // check if the line is longer than MAX_LINE_LENGTH
    // otherwise, read the first line into buffer1
    if (buffer1 > MAX_LINE_LENGTH) {
        perror("Error: The line read in is longer than the allocated buffer.\n");
        exit(EXIT_FAILURE);
    } else {
        // checking for end of line, then appending the null
        // character to the right of the last read char in buffer1
        // to indicate the line is over
        if (buffer1[i] == '\n' || buffer1[i] == 0x0) {
            buffer1[i] = '[=10=]';

            // write the first line in buffer1 to the terminal
            write(output_fd, buffer1, strlen(buffer1));
        }
    }
}

你可以这样做:

#include <stdio.h>                                                                 
#include <stdlib.h>                                                                
static void *                                                                      
xmalloc(size_t s)                                                                  
{                                                                                  
        void *p = malloc(s);                                                       
        if( p == NULL ){                                                           
                perror("malloc");                                                  
                exit(EXIT_FAILURE);                                                
        }                                                                          
        return p;                                                                  
}                                                                                  
int                                                                                
main(void)                                                                         
{                                                                                  
        char *buffer1 = xmalloc(256);                                              
        char *buffer2 = xmalloc(256);                                              
        buffer1[0] = buffer2[0] = '[=10=]';                                            
        buffer1[255] = buffer2[255] = 'x';                                         
        char *ip = buffer1;                                                        
        while( fgets(ip, 256, stdin) != NULL ){                                    
                if( ip[255] != 'x' ){                                              
                        fprintf(stderr, "Invalid input: line too long\n");         
                        exit(EXIT_FAILURE);                                        
                }                                                                                                     
                ip = ip == buffer1 ? buffer2 : buffer1;                            
        }                                                                          
        return 0;                                                                  
}

这一行

if (buffer1 > MAX_LINE_LENGTH) {

是错误的,因为它比较的是指针本身,而不是字符串的长度。这种情况很可能永远为真。因为你从不增加 i 你总是在同一个位置读取 1 个字节,所以至少不会因此而造成伤害,即使它可能不是你想要的。

int i = 0;
char *p = buffer1;
while (read(input_fd, &p[i], 1) == 1)
{
    if (p[i] == '\n')
    {
        p[i] = '[=11=]';
        write(output_fd, p, i);
        i = 0;
        if (p == buffer1)
           p = buffer2;
        else
           p = buffer1;
    }
    else
    {
        i++;
        if (i > MAX_LINE_LENGTH)
        {
            perror("Error: The line read in is longer than the allocated buffer.\n");
            exit(EXIT_FAILURE);
        }
    }
}