Let's say that I want scanf to process the following text
balance = 100
How do I skip over the word balance and the equal sign and capture only 100 and place it into an integer variable x?
Thanks.
Pier.
Printable View
Let's say that I want scanf to process the following text
balance = 100
How do I skip over the word balance and the equal sign and capture only 100 and place it into an integer variable x?
Thanks.
Pier.
i should read the whole thing with fgets and then convert the string with sscanf (+check for succesfull conversion)
>>Let's say that I want scanf to process the following text
You'll have fewer headaches if you don't consider using scanf, ever. Reading the whole line with fgets and then breaking it up with sscanf is much nicer. :-)
But if you really have to do it the ugly way, it's not much different.Code:#include <stdio.h>
int main(void)
{
int num;
char string[100];
/* Assume :string:ws:char:ws:number: */
if (fgets(string, 100, stdin) == 0)
{
return 1;
}
else if (sscanf(string, "%*s %*c %d", &num) == 1)
{
printf("Num -- %d\n", num);
}
return 0;
}
Code:#include <stdio.h>
int main(void)
{
int num;
/* Assume :string:ws:char:ws:number: */
if (scanf("%*s %*c %d", &num) == 1)
{
printf("Num -- %d\n", num);
}
return 0;
}
Thanks a lot guys :)
Pier.