Segmentation fault when calling fgets

后端 未结 2 524
轮回少年
轮回少年 2021-01-22 17:46

I\'m trying to write a simple program to read data from files listed in a plain text list, but I keep running into a segmentation fault when I try to call fgets() in my processF

相关标签:
2条回答
  • 2021-01-22 18:22

    fopen will return NULL if the file doesn't exist. Add some error handling after the call to fopen, it's possible you just have a bad entry in your list.

    What may be happening is that pgmFile is NULL, and fgets tries to access it, causing the seg fault.

    0 讨论(0)
  • 2021-01-22 18:25

    Try checking return value of fopen. What if it's NULL? Because it is likely that your "file name" has trailing \n.

    0 讨论(0)
提交回复
热议问题