C read file line by line

前端 未结 17 2166
后悔当初
后悔当初 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:10

    void readLine(FILE* file, char* line, int limit)
    {
        int i;
        int read;
    
        read = fread(line, sizeof(char), limit, file);
        line[read] = '\0';
    
        for(i = 0; i <= read;i++)
        {
            if('\0' == line[i] || '\n' == line[i] || '\r' == line[i])
            {
                line[i] = '\0';
                break;
            }
        }
    
        if(i != read)
        {
            fseek(file, i - read + 1, SEEK_CUR);
        }
    }
    

    what about this one?

提交回复
热议问题