So I'm writing a program and there is this bit of code where I have to take the user input in stdin and make a choice of what to do. I've taken the bit of code corresponding to it and made another program (that is bellow) to check if the error keeps on. And it does. The error is when doing if(sscanf(buffer, "%*s %d %s %s%c", &key, succ_ip, succ_gate, &eol) == 4 && eol == '\n')
, the second sscanf(), and i've figured that that sscanf() is != 4 witch was the value I was expecting since I'm asking for 4 variables.
The strange part is that it works just fine when strcmp(token, "new") == 0
but when strcmp(token, "sentry") == 0
it doesn't...
If anyone could help me I'dd be very thankful.
Here is the bit of my code where I'm having problems
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Max 150
int main(){
int key;
char* succ_ip;
succ_ip = (char*)malloc((Max+1)*sizeof(char));
char* succ_gate;
succ_gate = (char*)malloc((Max+1)*sizeof(char));
char* s_succ_ip;
s_succ_ip = (char*)malloc((Max+1)*sizeof(char));
char* s_succ_gate;
s_succ_gate = (char*)malloc((Max+1)*sizeof(char));
char* buffer;
buffer = (char*)malloc((5*Max+1)*sizeof(char));
char* token;
token = (char*)malloc((Max+1)*sizeof(char));
char eol = 0;
int block = 0;
int exit_flag = 0;
while(fgets(buffer, sizeof(buffer), stdin)){
sscanf(buffer, "%s", token);
/*NEW: creating the first server*/
if(strcmp(token, "new") == 0 && block == 0){
if(sscanf(buffer, "%*s %d%c", &key, &eol) == 2 && eol == '\n'){
/*strcpy(succ_ip, argv[1]);
strcpy(succ_gate, argv[2]);
strcpy(s_succ_ip, argv[1]);
strcpy(s_succ_gate, argv[2]);*/
block = 1;
printf("Chave : %d\n", key);
printf("-> Ring created.\n");
}
else{
printf("-> The command \\new is of type \"new i\". Where i is a key.\n");
fflush(stdin);
memset(buffer,0,sizeof(buffer));
memset(token,0,sizeof(token));
}
}
/*ENTRY: ... */
else if(strcmp(token, "entry") == 0 && block == 0){
/* do stuff */
block = 1;
printf("-> Server entered.\n");
}
/*SENTRY: adding a server specifying it's successor */
else if(strcmp(token, "sentry") == 0 && block == 0){
if(sscanf(buffer, "%*s %d %s %s%c", &key, succ_ip, succ_gate, &eol) == 4 && eol == '\n'){
/*test for unique case when there are only 2 servers*/
/*otherwise do the normal procedure*/
/*tcp_client = init_tcp_cl(succ_ip, succ_gate);
tcp_client = request_tcp_cl(tcp_client, "SUCCCONF\n");
close_tcp_cl(tcp_client);*/
printf("Chave : %d\n", key);
printf("Next server ip: %s\n", succ_ip);
printf("Next server ip: %s\n", succ_gate);
block = 1;
printf("-> Server sentered.\n");
}
else{
printf("-> The command \\sentry is of type \"sentry i succ.ip succ.gate\". Where i is a key.\n");
fflush(stdin);
memset(buffer,0,sizeof(buffer));
memset(token,0,sizeof(token));
}
}
/*LEAVE: ... */
else if(strcmp(buffer, "leave\n") == 0 && block == 1){
/* do stuff */
block = 0;
printf("-> Left the ring.\n");
}
/* FALTA ADICIONAR O ESTADO DO SERVIDOR!!! */
else if(strcmp(buffer, "show\n") == 0 && block == 1){
/*printf("-> Key: %d\n-> IP: %s\n-> PORT: %s\n-> SuccIP: %s\n"
"-> SuccPORT: %s\n", key, argv[1], argv[2],
succ_ip, succ_gate);*/
}
/*FIND: ... */
else if(strcmp(token, "find") == 0){
/* do stuff */
}
/*EXIT: exits the application successfully*/
else if(strcmp(buffer, "exit\n") == 0){
printf("\nExiting the application...\n");
exit_flag = 1;
}
/*Invalid command, ignores it*/
else{
printf("-> Invalid command.\n");
}
}
return 1;
}