C read file line by line

前端 未结 17 2349
后悔当初
后悔当初 2020-11-22 03:45

I wrote this function to read a line from a file:

const char *readLine(FILE *file) {

    if (file == NULL) {
        printf(\"Error: file pointer is null.\"         


        
17条回答
  •  长发绾君心
    2020-11-22 04:20

    My implement from scratch:

    FILE *pFile = fopen(your_file_path, "r");
    int nbytes = 1024;
    char *line = (char *) malloc(nbytes);
    char *buf = (char *) malloc(nbytes);
    
    size_t bytes_read;
    int linesize = 0;
    while (fgets(buf, nbytes, pFile) != NULL) {
        bytes_read = strlen(buf);
        // if line length larger than size of line buffer
        if (linesize + bytes_read > nbytes) {
            char *tmp = line;
            nbytes += nbytes / 2;
            line = (char *) malloc(nbytes);
            memcpy(line, tmp, linesize);
            free(tmp);
        }
        memcpy(line + linesize, buf, bytes_read);
        linesize += bytes_read;
    
        if (feof(pFile) || buf[bytes_read-1] == '\n') {
            handle_line(line);
            linesize = 0;
            memset(line, '\0', nbytes);
        }
    }
    
    free(buf);
    free(line);
    

提交回复
热议问题