scanf - identifying characters from an input file in C -
i have input file looks this:
5 (2,3) (1,4) (1,3) (3,4) (4,5) the first number number of ordered pairs. i'm trying scan in each x in (x,y) in array x[i], , same y digit y[i]. tried use fscanf take each line , put array, set x[i]=array[1] , set y[i]=array[3] each i, think going on array[0] not equal '(' each time scanned in. simplest way scan in x , y in (x,y) x[i]=x , y[i]=y each ordered pair?
if numbers in pairs integers, can scan them loop:
int i, res, x[number], y[number]; (i = 0; < number; i++) { if (scanf(" (%d ,%d )", &x[i], &y[i]) != 2) { printf("invalid input\n"); exit(1); } } the initial space in format consume whitespace characters, such linefeed left in input stream previous scanf.
as suggested kemyland, other spaces after %d ignore spaces between number , , , ) characters. ignoring spaces before numbers done %d format. should not add 1 after ) because instruct scanf consume spaces after pair, including \n, not problem, prompting more input until non space character has been typed, not want.
scanf old , clunky function, can used quick , dirty parsing, has many quirks , shortcomings, study manual , @ many examples.
Comments
Post a Comment