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

发布于 2024-09-02 00:28:04 字数 324 浏览 4 评论 0原文

我有一些分析代码(myprog),它使用以下命令吸收数据:

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

效果很好。但在我有用逗号分隔的数据文件的情况下,我目前正在执行以下操作:

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

我可以修改 fscanf() 函数中的格式字符串以接受两者分隔格式?

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走过海棠暮 2024-09-09 00:28:04
fscanf(in, "%[^, ]%*[, ]%lf%*[, ]%f%*[, ]%f%*[, ]%f", tag, &sec, &tgt, &s1, &s2)

应该工作吗?

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

Should work?

久而酒知 2024-09-09 00:28:04

这个怎么样:

char tmp;
fscanf(in, "%s%c%lf%c%f%c%f%c%f", 标签, &tmp, & 秒, &tmp,& tgt, &tmp,& s1 , &tmp, & s2)

如果您不关心什么单个字符分隔您的值,只需读取它并将其存储在一次性变量中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文