I have written a program in C which lets the user enter a password to let him into the system or not. I am using the method fgets. The correct password is “letmein”. This is the code:
Now I want to verify that the password entered by the user through stdin is no longer than 8 characters. As the program is (without the empty if statement), the user is granted access even if he entered “letmein0000000” since only the first seven characters are fetched by fgets. Now I only want to grant access to the user if he enters “letmein”. How can this be done please?
P.S. I have to use fgets since it is a requirement in my project.
From documentation for
fgets():Request to read a maximum of
8characters (this means passing9as second argument). If the user enters more than7characters then it can be caught. Inform user of failure and skip whatever remains instdin. If the user correctly enters7and hits returnfgets()will stop at the new-line character.Check return value of
fgets()to ensure the code does not attempt to process an unitialised buffer.For example: