Passando parâmetros com getopt
Publicado por Andre Luiz da Silva 02/05/2004
[ Hits: 11.581 ]
Homepage: http://localhost
Esse é um simples código demonstrando como manipular parâmetros passados para um programa usando a função "getopt".
#include <stdio.h> #include <unistd.h> #include <errno.h> void help() { printf ("Olah.... eu sou o a funcao help!"); } void verbose () { printf ("Olah... eu sou a verbose!! \n"); } int print (char *file, char *string) { FILE *fd; if ((fd = fopen (file, "w")) == NULL) { printf ("fopen(): %s \n", strerror(errno)); exit (1); } fprintf (fd, "%s", string); fclose (fd); return 0; } int opterr = 0; int main(int argc, char **argv) { char *file = NULL, *phase = NULL; if (argc == 1) { printf ("Nao tem argumento! Falowz... \n"); exit (0); } while (1) { int opt; opt = getopt (argc, argv, "hvp:f:"); switch (opt) { case 'h': help(); break; case 'v': verbose(); break; case 'p': phase = optarg; break; case 'f': file = optarg; break; } if (opt == -1) break; if (opt == '?') { printf ("%c: This option don't exists!\n", optopt); break; } } if (phase != NULL && file != NULL) { print (file, phase); exit(0); } return 0; }
Calculo calorico visando perca de gordura e definicao muscular
Um algoritmo genético para o TSP (Travel Salesman Problem)
Como escolher o melhor escalonador de CPU para melhorar o desempenho da máquina
Curiosidade sobre DOOM Guy e Isabelle de Animal Crossing
Inicializando servidor Ubuntu na AWS e rodando apache em Container
Conheça o Octopi, outro frontend para o Pacman com acesso ao ARU (Arch Linux e derivados)
Terminal transparente no Debian 12 com interface i3wm usando Xfce4-Terminal e Compton
Como compilar o DOOM RETRO no Linux
Stacer - um otimizador gráfico básico de Linux
Corrigindo o erro de "WARNING: Possibly missing firmware for module" no Arch Linux
Ubuntu simplesmente morreu (7)
Como personalizar meu Linux Mint xfce? (10)
DRIVER DE ADPTADOR HDMI PARA USB (1)