How do I use fgets instead of gets?

0

Before "talking" about my problem, first look at my code and ignore the accentuation errors in the console if it is to execute.

#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");
  }
}

WhenItype"0" with the fgets command, the IF does not find "0", BUT when I type "0" through gets .

I want to avoid gets , as many websites / books recommend not to use this command, as it is dangerous and gives buffer error. (so far, I've had no buffer-related issues) but infezlimente fgets is not working as I wanted, only gets is working in my codes, but I want to avoid it.

Does anyone know how to make fgets have the characters detected by the

    
asked by anonymous 03.09.2016 / 15:17

1 answer

1

The fgets function puts '\ n' at the end of the read string. So in this case, the content of "name" will be "0 \ n", not just "0".

link

    
03.09.2016 / 15:22