Reading from files passed as command line arguements

前端 未结 3 2028
暗喜
暗喜 2021-01-21 16:15

I am trying to parse a given textfile, but so far, my program does not seem to be reading properly.

#include 


int main(int argc, char *argv[])
{         


        
3条回答
  •  暖寄归人
    2021-01-21 16:22

    Your code looks clear and straight-forward, but there is one important thing missing: error handling.

    What happens if the file you want to open does not exist? fopen returns NULL in that case.

    What happens if the file does not start with a number? fscanf returns the number of fields that have been successfully read, so you should check that the return value is at least 1.

    You need to somehow handle these cases, probably by printing some error message and exiting the program. When you do that, be sure to include the relevant information in the error messages. Then you will find the bug that the other answers have already mentioned.

提交回复
热议问题