Ver Mensaje Individual
  #6 (permalink)  
Antiguo 16/01/2014, 09:42
carbon
 
Fecha de Ingreso: enero-2012
Ubicación: Buenos Aires
Mensajes: 745
Antigüedad: 12 años, 3 meses
Puntos: 35
Respuesta: No me anda la funcion fgets en c

Que te parece algo así?

Código C:
Ver original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void get_input(const char *prompt, char *buffer)
  6. {
  7.     int bytes = 0;
  8.     char current_char;
  9.     char *input = NULL;
  10.  
  11.     printf("%s", prompt);
  12.  
  13.     while ((current_char = getchar()) != '\n')
  14.     {
  15.         bytes++;
  16.         input = (char *)realloc(input, bytes);
  17.         input[bytes - 1] = current_char;
  18.     }
  19.  
  20.     input[bytes] = '\0';
  21.  
  22.     strcpy(buffer, input);
  23.  
  24.     free(input);
  25. }
  26.  
  27. int main()
  28. {
  29.     char nombre[20];
  30.  
  31.     get_input("Escribe tu nombre: ", &nombre);
  32.     printf("Tu nombre es: %s\n", nombre);
  33.  
  34.     return 0;
  35. }

Espero que te sirva!