c - 用 C 读取高分/分析字符串

标签 c string sorting

我有一个存储名称和分数的高分文件。每个条目除以 ;名称和分数之间是 - 这是一个例子:

15-Player One;10-Player Two;20-Player Three;10-Player Four;8-Player Five

我现在正在将文件的内容读入char *缓冲区

我现在的目标是将每个条目与另一个条目分开[有点像 C# 的 buffer.Split(';')] 并按升序打印列表。

有关于如何以最简单的方式做到这一点的专业提示吗?我目前正处于停电状态...

最佳答案

在 string.h 中,您可以使用 strtok() :

#include <string.h>
#include <stdio.h>

int main()
{
    char scores[256] = "15-Player One;10-Player Two;20-Player Three;10-Player Four;8-Player Five";
    const char separator[2] = ";";
    char *entry;

    // Get the first entry
    entry = strtok(scores, separator);

    // Get all the others
    while (NULL != entry)
    {
        printf("%s\n", entry);

        /* You can also use it here to separate the actual score from the player */

        entry = strtok(NULL, separator);
    }

    return 0;
}

关于c - 用 C 读取高分/分析字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39261499/

相关文章:

c - char 变量在不接受输入的情况下继续进行

c++ - 为什么我们在c语言循环的退出函数中使用任何值

c - wchar_t valgrind 问题 - 大小 8 的无效读取

python - 对列表进行排序,其中某些值保持在固定位置

结构中的 C 字符串

java - HTML 电子邮件的数据绑定(bind)

java - 文件行中的 bin 编号可以优雅地分组

python - 从列表中仅获取所需的字符串

java - Angular : How to sort List of Objects by property?

python - Bentley-McIlroy 3 路分区