C - stdin to variables -
i want create program can read lines standard input , interpret each line 2 integer numbers separated space. later use shared library work numbers . before start know, best way parse numbers lines. combination of "getline" , storing numbers variables? store numbers until next numbers given.
if want extract integers string,use
sscanf(arr,"%d %d",&num1,&num2);
where arr
array of char containing data want extract 2 integers , num1
, num2
2 int
variables.
or else can directly scan 2 int
s using
scanf("%d %d",&num1,&num2);
it better check if scanf
(or sscanf
) successful. both of these return 2(in case) if successful. check using like:
if(scanf("%d %d",&num1,&num2)==2) //successful else //failed
or if use sscanf
,
if(sscanf(arr,"%d %d",&num1,&num2)==2) //successful else //failed
Comments
Post a Comment