开发者

How can I create an fscanf format string to accept white space and comma (,) tokenization

开发者 https://www.devze.com 2022-12-30 17:04 出处:网络
I\'ve got some analysis code (myprog) that sucks in data using the following: if(5 == fscanf(in, \"%s%lf%f%f%f\", tag, & sec, & tgt, & s1, & s2))

I've got some analysis code (myprog) that sucks in data using the following:

 if(5 == fscanf(in, "%s%lf%f%f%f", tag, & sec, & tgt, & s1, & s2))

which works just fine. But in the situation where I've got data files that are separated by co开发者_StackOverflow中文版mmas, I'm currently doing something like:

 sed 's/,/ /g' data | myprog

Can I modify the format string in the fscanf() function to accept both delimitation formats?


fscanf(in, "%[^, ]%*[, ]%lf%*[, ]%f%*[, ]%f%*[, ]%f", tag, &sec, &tgt, &s1, &s2)

Should work?


What about this:

char tmp; fscanf(in, "%s%c%lf%c%f%c%f%c%f", tag, &tmp, & sec, &tmp,& tgt, &tmp,& s1, &tmp, & s2)

If you are not going to care what single character delimits your values, just read it and store it in a throw-away variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消