I have a problem parsing in c with sscanf
I read a text on console with one function called read_line()
char cm1[100],cm2[100],cm3[100]
printf("Enter command:");
read_line(var_text);
/*var_text = cat /etc/passwd | cut -f1 d: | sort */
int num = sscanf(var_text,"%s | %s | %s",cm1,cm2,cm3);
Ok if i write in var_text cat | cut | sort in cm1 return cat in cm2 return cut and in cm3 return sort, but if i write cat /etc/passwd | cut -f1 d: | sort , cm1 return cat and cm2 and cm3 none…
I’m made a shell in c, and i need the commands and atributes
Thx for all, and sorry for that bad english 🙂
The
%sformat specifier will stop processing at the first whitespace character. In the case of:it will be the space after the
catat which the first%swill end. The format specifier then expects a|which does not exist andcm2andcm3are not populated. You could use a scanset to achieve this:Note use of width specifier to prevent buffer overrun and checking of return value from
sscanf()to ensure all target variables were populated.The format specifier
%99[^|]is stating read at most 99 characters (one less than the target buffer size to accomodate the null terminator) until a|character is encountered.See demo at http://ideone.com/Cz1Qef .