Hi I am having problems with sscanf. I am just trying to write a simple test program to help me understand how it works. The problem arises when writing to differents variables with sscanf. Here is what their values should be:
IP=175.85.10.147
number=2589
email = [email protected]
#include <stdio.h>
#include <stdlib.h>
int main() {
char *toSend = "175.85.10.147:2589:[email protected]";
int *number;
char IP[200];
char email[300];
sscanf(toSend, "%[^:]: %i%[^:]: ", IP, number, email);
printf("%s", IP);
printf("%i", number);
printf("%s", email);
return 0;
}
The IP it prints correct.
The number doesn't print correctly, which might be related to this warning I'm getting at compile time: format '%i' expects argument of type 'int' but argument 2 has type 'int *'.
The email variable just contains bizarre characters for some reason.
You need some memory to store number
i.e. change
int *number;
to
int number;
Then use
sscanf(toSend,"%[^:]:%i:%[^:]",IP, &number, email);
I have also correct the typos - here is the code http://ideone.com/hpqGDZ
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main(){
char *toSend = "175.85.10.147:2589:[email protected]";
int number;
char IP[200];
char email[300];
if (sscanf(toSend, "%199[^:]:%i:%299[^:]", IP, &number, email) != 3) {
return 1;
}
printf("%s\n", IP);
printf("%i\n", number);
printf("%s\n", email);
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With