Trouble reading a line using fscanf()

后端 未结 7 747
暖寄归人
暖寄归人 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)
    
    0 讨论(0)
提交回复
热议问题