How do I use fgets instead of gets?
Before "talking" about my problem, look at my code first and ignore the accentuation errors in the console if it is going to run.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main(){
char nome[41];
printf("Texto: ");
fgets(nome, 40, stdin);
if (strcmp(nome, "0") == 0) {
printf("Sim. fgets está funcionando.\n");
} else {
printf("Não. fgets não está funcionado.\n");
}
printf("Texto: ");
gets(nome);
if (strcmp(nome, "0") == 0){
printf("Sim. gets está funcionando.\n");
} else {
printf("Não. gets não está funcionando.\n");
}
}
When I type " 0 " by the command fgets , IF does not find "0", but when I type "0" by the gets IF finds "0".
I want to avoid the gets, as many sites/books recommend not to use this command, as the same is dangerous and gives buffer error. (so far, I don't I had problems related to buffer) but infezlimente fgets is not working as I wanted, only gets is working in my codes, but I want to avoid.
Does anyone know how to make fgets have the characters be detected by the IF
0
Author: Ryo Akiyama, 2016-09-03
1 answers
The fgets function puts '\n ' at the end of the read string. So, in your case, the content of "name "will be" 0\n", and not just"0".
1
Author: zentrunix, 2016-09-03 13:22:18