逐行读取文件,read()会抓取整个文件

时间:2019-04-17 03:43:39

标签: c pthreads system-calls

我无法逐行读取文件。显然,read()系统调用将获取整个文件。我正在尝试读取长度可变的行的文件,但是我知道行的长度不能超过SBUFSIZE个字节。我应该读取文件中的每一行并将文件的每一行放入数据结构中。但是,我的方法将整个文件作为一行推送到数据结构中,这是不可接受的。是否有read()的修改版本,以'\n'字符结尾?

#define SBUFSIZE 1025

pthread_mutex_t buffer_lock;

void* process_file(void* file_name)
{
    int input_fd;
    /* Temporary buffer, for reading in the files, one line at a time. */
    char buf[SBUFSIZE];
    memset(buf, '\0', SBUFSIZE);

    if ((input_fd = open((char*) file_name, O_RDONLY)) == -1) {
        fprintf(stderr, "Cannot open the file '%s'\n", (char*) file_name);
        pthread_exit((void*) 1);  /* This is my error flag. */
    }

    while (read(input_fd, buf, SBUFSIZE)) {
        int ret;
        printf("|%s|\n", buf);
        while (true) {
            pthread_mutex_lock(&buffer_lock);
            ret = stack_push(buf);
            if (ret == STACK_FULL) {
                pthread_mutex_unlock(&buffer_lock);
                usleep(rand() % 101);
            } else {
                break;
            }
        }
        pthread_mutex_unlock(&buffer_lock);
        memset(buf, '\0', SBUFSIZE);

        if (ret != STACK_SUCCESS) {
            exit(EXIT_FAILURE);
        }
    }

    close(input_fd);
    pthread_exit((void*) 0);  /* This is my good flag. */
}

1 个答案:

答案 0 :(得分:1)

您可以按以下方式逐行处理:

char buf[SBUFSIZE + 1];
size_t bufsize = 0;

for(;;)
{
    ssize_t nread = read(input_fd, buf + bufsize, SBUFSIZE - bufsize);
    if(nread < 0)
        perror("read failed");

    bufsize += nread;
    if(!bufsize)
        break; // end of file

    const char *eol = memchr(buf, '\n', bufsize);
    if(!eol)
        eol = buf + bufsize++;
    *eol = 0;

    printf("processing line: |%s|\n", buf);
    process_line(buf);

    ++eol;
    bufsize -= eol - buf;
    memmove(buf, eol, bufsize);
}