简体   繁体   English

如何使用 C 中的系统调用读取文件行直到结束?

[英]How to read line of file until end using system calls in C?

I'm trying to read a file line by line by comparing the currently read character with \n (end of line symbol).我试图通过将当前读取的字符与\n (行尾符号)进行比较来逐行读取文件。 The characters of that line are stored in a buffer.该行的字符存储在缓冲区中。

I'm confused how to handle at the same time the buffer, where I keep the current line and the check with the end of line character.我很困惑如何同时处理缓冲区,我保留当前行并使用行尾字符进行检查。 That's what I use the indexCurrentSymbol for, to always the the last symbol of the buffer and see if it's \n .这就是我使用indexCurrentSymbol的目的,始终是缓冲区的最后一个符号,看看它是否是\n

Please not that I'm supposed to achieve this with basic system calls (so no library functions that read a full line).请不要说我应该通过基本的系统调用来实现这一点(所以没有读取整行的库函数)。

The input is输入是

Animal, Size
Dog, Big
Duck, Small
Mouse, Tiny
Horse, Huge

I'm ignoring all error checks for files that might not be available on purpose to be able to focus on the task.我忽略了对可能不可用的文件的所有错误检查,以便能够专注于任务。

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

int main(int argc, char *argv[]) { 

    // Open file to read line.
    int input = open(argv[1], O_RDONLY);
    char buf[100];
    int read_bytes = read(input, &buf, 1); 
    int indexCurrentSymbol = 0;
    while (buf[indexCurrentSymbol] != '\n') {
        // read(input, buf, 1);    // Correction based on comment.
        read(input, &buf[indexCurrentSymbol], 1);
        indexCurrentSymbol++;
    }   

    close(input);
    return 0;
}

Here is a modified program version that allows to read the whole file (note that error processing is not enough and there is no check on buf maximum size):这是一个修改后的程序版本,它允许读取整个文件(注意错误处理是不够的,并且没有检查buf最大大小):

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

int main(int argc, char *argv[]) { 


    char buf[100];
    int indexCurrentSymbol = 0;
    int read_bytes;
    int line_read=0;
    int file_read=0;

    // Open file to read line.
    int input = open(argv[1], O_RDONLY);
    if (input < 0) 
    perror("open");

    buf[0] = '\0';
    while (file_read == 0) {

        read_bytes = read(input, &buf[indexCurrentSymbol], 1);
        if (read_bytes == -1) 
           perror("read while: -1");
        if (read_bytes == 0) 
            file_read = 1;

        if (buf[indexCurrentSymbol] == '\n')
             printf("current buf=\n%s\n", buf);

        indexCurrentSymbol++;
    }   

    printf("final buf=\n%s\n", buf);
    close(input);
    return 0;
}

With:和:

$ cat tread.dat
Animal, Size
Dog, Big
Duck, Small
Mouse, Tiny
Horse, Huge

I get:我得到:

$ ./tread tread.dat
current buf=
Animal, Size

current buf=
Animal, Size
Dog, Big

current buf=
Animal, Size
Dog, Big
Duck, Small

current buf=
Animal, Size
Dog, Big
Duck, Small
Mouse, Tiny

current buf=
Animal, Size
Dog, Big
Duck, Small
Mouse, Tiny
Horse, Huge

final buf=
Animal, Size
Dog, Big
Duck, Small
Mouse, Tiny
Horse, Huge

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM