简体   繁体   中英

check for error while reading file though fgets()

I want to know how I would detect an error on fgets() , I am writing test class, so I want to check whether the user entered the wrong buffer size on fgets() , and I want to display a message and do not make the program exit on its own

char a[10];

fgets(a,100,stdin);

I want to make the program exit on status 2 as I will clarify that error happens

The programming issue is better solved by good programming practices, static analysis, testing and debugging before letting users loose on it. The kind of errors you need to write code for are those that can happen at runtime because of erroneous input . If you write undetected bugs in your code, writing more code to detect them makes little sense.

In your example, a better coding practice to mitigate the chances of coding error is:

char a[10] ;

fgets( a, sizeof(a), stdin ) ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM