Making fscanf Ignore Optional Parameter

Posted by adi92 on Stack Overflow See other posts from Stack Overflow or by adi92
Published on 2008-10-29T04:32:21Z Indexed on 2010/05/08 13:48 UTC
Read the original article Hit count: 172

Filed under:
|

I am using fscanf to read a file which has lines like
Number <-whitespace-> string <-whitespace-> optional_3rd_column

I wish to extract the number and string out of each column, but ignore the 3rd_column if it exists

Example Data:
12 foo something
03 bar
24 something #randomcomment

I would want to extract 12,foo; 03,bar; 24, something while ignoring "something" and "#randomcomment"

I currently have something like

while(scanf("%d %s %*s",&num,&word)>=2)
{ 
assign stuff 
}

However this does not work with lines with no 3rd column. How can I make it ignore everything after the 2nd string?

© Stack Overflow or respective owner

Related posts about c

    Related posts about scanf