简体   繁体   English

如何创建fscanf格式的字符串以接受空格和逗号(,)标记

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

I've got some analysis code ( myprog ) that sucks in data using the following: 我有一些分析代码( myprog ),使用以下代码吸收数据:

 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 commas, 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()函数中修改格式字符串以接受两种分隔格式吗?

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

应该管用?

What about this: 那这个呢:

char tmp; 字符tmp; fscanf(in, "%s%c%lf%c%f%c%f%c%f", tag, &tmp, & sec, &tmp,& tgt, &tmp,& s1, &tmp, & s2) fscanf(在“%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. 如果您不关心哪个单个字符界定您的值,则只需阅读该字符并将其存储在一次性变量中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM