Also, when you call fgets, it makes for better maintenance to use sizeof instead. And there's a chance that the user entered a line that was too long for your buffer, you will probably want to check for this too:
char buf[33];
FILE *file;
/* Open the file */
if (NULL==fgets(buf, sizeof buf, file)) {
/* fgets() failed, handle error */
} else {
char *tmp=strchr(buf,'\n');
if (NULL!=tmp) {
/* success, get rid of the newline */
*tmp='\0';
} else {
/* line was truncated, handle accordingly */
}
}
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.