Jump to content

Função para proteger o input de dados


Recommended Posts

podes usar a combinação fgets/sscanf:

int input(FILE * stream, int * result)
 int valid = 0;
 char buffer[256];

 // falta a verificacao de argumentos

 while (!valid) // ciclo de leitura
   if (fgets(buffer, 256, stream) != NULL) // ler uma linha do stream
     if (sscanf(buffer, "%d", result) == 1) // interpretar a linha lida
       valid = 1; // leitura valida

 return 0;

nota : isto é uma opção de leitura bloqueante, mas podes tirar ideias

  • Vote 1
IRC : sim, é algo que ainda existe >> #p@p
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.