Hi all,
I know scanf can generate some input errors, and gets should be avoided at all cost, so I wonder if someone can sugest a work around for a problem I have.
I'm using rcpgen to generate client and server stubs for a remote call procedure project I'm doing in school, everything is working fine, except for some times when i use scanf for getting input from keyboard, i know the problem is there because if i use argv[] to get that input from command line, or initialize the variables with some values, everything works fine.
The code main():
Code:
main(argc,argv)
int argc; char *argv[];
{
namelist nl;
char *server;
char operacao;
if (argc!=2)
{
fprintf(stderr,"Sintaxe correcta: %s <host>\n",argv[0]);
exit(1);
}
server = argv[1];
/* -------------------- MENU PRINCIPAL ---------------------- */
printf("\n\n\n******************************************************\n");
printf("* MENU PRINCIPAL *\n");
printf("* *\n");
printf("* a) Listar directorio do servidor = DIR *\n");
printf("* b) Mudar directorio do servidor = CD *\n");
printf("* c) Mudar directorio do servidor = PWD *\n");
printf("* d) Copiar Ficheiro do servidor = GET *\n");
printf("* e) Copiar Ficheiro para o servidor = PUT *\n");
printf("* *\n");
printf("******************************************************\n\n");
printf("Opcao: ");
scanf ("%c",&operacao);
switch (operacao)
{
case 'a':
case 'A':
listaDirectorio((novaLigacao(server)),server, nl);
break;
case 'b':
case 'B':
defineDIR(server);
break;
case 'c':
case 'C':
mostraDIR(server);
break;
default:
printf("\nA opcao introduzida nao e valida\n");
}
}
when selection 'b' option i use a function similiar to a CD linux command (change directory on server)
Code:
/* LIKE 'CD' CHANGES DIR ON SERVER */
defineDIR(char *server)
{
CLIENT *clnt;
int *result;
char *dir;
printf("\nDirectory: ");
scanf("%s",dir);
/* Calls novaLigacao function that creates a new connection to server */
clnt = novaLigacao(server);
/* set's the dir on the server */
result = fixadir_1(&dir, clnt);
if (result == NULL)
{
clnt_perror(clnt, server);
exit(1);
}
/* cleaning */
clnt_destroy(clnt);
exit(0);
}
if I initialize the char *dir with for example "/root" like the code below, my code works fine, and the server get his dir defined as "/root" so the problem must be on scanf function and getting the input from the keyboard, also before you say it, the fflush(stdin) doesn't changes anything.
This way it works:
Code:
/* LIKE 'CD' CHANGES DIR ON SERVER */
defineDIR(char *server)
{
CLIENT *clnt;
int *result;
char *dir="/root";
/* Calls novaLigacao function that creates a new connection to server */
clnt = novaLigacao(server);
/* set's the dir on the server */
result = fixadir_1(&dir, clnt);
if (result == NULL)
{
clnt_perror(clnt, server);
exit(1);
}
/* cleaning */
clnt_destroy(clnt);
exit(0);
}
The error i get is like this: "segmentation fault"
Help this newb please.
Thanks for your time.