如何在 C 中标记/拆分包含日期和随机数的 .csv 文件中的数据?
我正在尝试对包含随机日期和数字的 c 文件中的数据进行标记。 例如数据:
Thursday,60
Tuesday,45
Wednesday,80
Monday,14
Saturday,73
Tuesday,3
Saturday,29
.
.
.
Friday,71
Saturday,98
我的主要目的是获取这些数据并执行以下操作:
Sunday: (Total of numbers sunday has in data)
Monday: (Total of numbers monday has in data)
Tuesday: (Total of numbers tuesday has in data)
.
.
.
Saturday: (Total of numbers saturday has in data)
I am trying to tokenize data from c file containing random days and numbers.
For example, data:
Thursday,60
Tuesday,45
Wednesday,80
Monday,14
Saturday,73
Tuesday,3
Saturday,29
.
.
.
Friday,71
Saturday,98
My main intention is to grab these data and do like:
Sunday: (Total of numbers sunday has in data)
Monday: (Total of numbers monday has in data)
Tuesday: (Total of numbers tuesday has in data)
.
.
.
Saturday: (Total of numbers saturday has in data)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议尝试 strtok
I would recommend trying strtok
strtok
是一个 C 标准函数,正是您正在寻找的函数。strtok
is a C Standard function and is what you are looking for.解决此类问题的常见方法是:
1) 读入数据 (stdio.h)。请参阅http://www.acm.uiuc.edu/webmonkeys/book/ c_guide/2.12.html
2) 使用正则表达式获取星期几(regex.h)。请参阅 http://www.gnu.org/software/libc/手册/html_node/Regular-Expressions.html。在这种情况下,您可以编写一个非常简单的正则表达式。
3) 有一个大小为 7 的 int[]
4) 使用 (2) 中的正则表达式获取数字并递增数组的相应元素
A common approach to a problem like this is to:
1) Read in the data (stdio.h). See http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.12.html
2) Use Regular expressions to get the day of the week (regex.h). See http://www.gnu.org/software/libc/manual/html_node/Regular-Expressions.html. In this case, you could write a really simple regex.
3) Have a int[] of size 7
4) Use the regex from (2) to get the number and increment the corresponding element of the array
fgets
将每一行读入缓冲区 使用strtok
将缓冲区标记化 使用strtol
将数字标记转换为整数值这就是最简单的事情。
fgets
strtok
strtol
That's about as simple as it gets.