Trouble reading a line using fscanf()

后端 未结 7 758
暖寄归人
暖寄归人 2020-12-01 15:40

I\'m trying to read a line using the following code:

while(fscanf(f, \"%[^\\n\\r]s\", cLine) != EOF )
{
    /* do something with cLine */
}

7条回答
  •  悲哀的现实
    2020-12-01 16:05

    i think the problem with this code is because when you read with %[^\n\r]s, in fact, you reading until reach '\n' or '\r', but you don't reading the '\n' or '\r' also. So you need to get this character before you read with fscanf again at loop. Do something like that:

    do{
        fscanf(f, "%[^\n\r]s", cLine) != EOF
    
        /* Do something here */
    
    }while(fgetc(file) != EOF)
    

提交回复
热议问题