如何在 C 中标记/拆分包含日期和随机数的 .csv 文件中的数据?

发布于 2025-01-07 14:44:59 字数 433 浏览 2 评论 0原文

我正在尝试对包含随机日期和数字的 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 技术交流群。

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

发布评论

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

评论(4

意犹 2025-01-14 14:44:59

我建议尝试 strtok

I would recommend trying strtok

千纸鹤带着心事 2025-01-14 14:44:59
$ man -k token
strsep (3)           - extract token from string
strtok (3)           - extract tokens from strings
strtok_r (3)         - extract tokens from strings
tc-htb (8)           - Hierarchy Token Bucket
tc-tbf (8)           - Token Bucket Filter
wcstok (3)           - split wide-character string into tokens
$

strtok 是一个 C 标准函数,正是您正在寻找的函数。

$ man -k token
strsep (3)           - extract token from string
strtok (3)           - extract tokens from strings
strtok_r (3)         - extract tokens from strings
tc-htb (8)           - Hierarchy Token Bucket
tc-tbf (8)           - Token Bucket Filter
wcstok (3)           - split wide-character string into tokens
$

strtok is a C Standard function and is what you are looking for.

坚持沉默 2025-01-14 14:44:59

解决此类问题的常见方法是:

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

所谓喜欢 2025-01-14 14:44:59
  1. 使用 fgets 将每一行读入缓冲区 使用
  2. strtok 将缓冲区标记化 使用
  3. strtol 将数字标记转换为整数值
  4. 将该值添加到运行中那一天的总计

这就是最简单的事情。

  1. Read each line into a buffer using fgets
  2. Tokenize the buffer using strtok
  3. Convert the number token to an integer value using strtol
  4. Add that value to the running total for that particular day

That's about as simple as it gets.

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