Traverse FILE line by line using fscanf

后端 未结 4 837
情话喂你
情话喂你 2021-01-03 03:08

Ok so i have a text file database.txt.

Each line is a user with the format below

\"John Smith\"| 4| 80.00| \"123 Lollipop Lane\"| \"New Jersey\"| \         


        
4条回答
  •  难免孤独
    2021-01-03 03:49

    The problem is that your fscanf will never read the newline at the end of the first line. So when it is called the second time, it will fail (returning 0, not EOF) and read nothing, leaving buffer unchanged.

    You could add a call to fscanf("%*[\n]"); at the end of your while loop to skip the newline (and any blank lines that might occur). Or you could just use fgets instead, which also makes it easier to avoid the potential buffer overflow problem.

提交回复
热议问题