【问题标题】:How can I read lines from a .txt file into two separate buffers?如何将 .txt 文件中的行读入两个单独的缓冲区?
【发布时间】:2021-04-01 20:06:54
【问题描述】:

我有两个缓冲区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] = '\0';

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

【问题讨论】:

  • 您可以使用指向缓冲区的指针,而不是直接使用缓冲区。你想只读取两行,还是读取任意数量的行并交替读取?
  • @Devolus 我想读取多行直到 EOF,同时交替使用 b/w buffer1 和 buffer2。例如,“这是第一句话”。将被读入 buffer1 和下一个相邻的句子,比如“这是下一个句子”。将被读入 buffer2 等等,直到 EOF。
  • "buffer1 > MAX_LINE_LENGTH" - 你是从哪里得到这个想法的?我想编译器警告过你。

标签: c file-io system-calls


【解决方案1】:

这一行

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] = '\0';
        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);
        }
    }
}

【讨论】:

  • 你能解释一下if (p == buffer1)在做什么吗?
  • 如果您当前在 buffer1 上运行,它会切换到 buffer2,否则会切换到 buffer2。这会比较当前正在使用的缓冲区指针。
【解决方案2】:

你可以这样做:

#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] = '\0';                                            
        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;                                                                  
}

【讨论】:

  • 您好,感谢您的回复。不幸的是,我是 C 新手,所以我不太了解您的源代码在做什么。你能解释一下吗?另外,我只能使用系统调用,因为这是用于分配。
  • 如果您使用read 而不是fgets,则实际上不可能将每一行直接读入适当的缓冲区。也许您应该实现fgets 功能。找到换行符后,您需要读入通用缓冲区并将数据复制到缓冲区 1 或缓冲区 2 中。或者也许您应该一次只将一个字符读到适当的位置。即,当你看到'\n' 时,切换ip 指向的缓冲区。
猜你喜欢
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 2018-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-21
  • 2021-09-26
相关资源
最近更新 更多