sscanf 解析格式化字符串

sscanf parse formatted string

我想读取一个包含未定义数量后缀的字符串,所有后缀由 ;

分隔

example 1: « .txt;.jpg;.png »

example 2: « .txt;.ods;_music.mp3;.mjpeg;.ext1;.ext2 »

browsed the web 并写了那段不起作用的代码:

char *suffix[MAX]; /* will containt pointers to the different suffixes */
for (i = 0; i < MAX ; i++)
{
    suffix[i] = NULL;
    if (suffix_str && sscanf(suffix_str,"%[^;];%[^\n]",suffix[i],suffix_str) < 1)
        suffix_str = NULL;
}

第一次迭代后,sscanf的结果为0,为什么没有读取字符串的内容?

应该如何解析包含未定义数量元素的字符串? sscanf 是个不错的选择吗?

有多种方法可以从 C 字符串中进行标记化。除了使用 strtoksscanf 你也可以这样做:

char *temp = suffix_str;
char *suffix[i];
for (int i = 0; i < MAX; i++)
{
    int j = 0;
    char buf[32];
    while (*temp != '[=10=]' && *temp != '\n' && *temp != ';')
    {
        buf[j++] = *temp;
        temp++;
    }
    buf[j] = 0;

    if (*temp == ';') temp++;

    suffix[i] = malloc((strlen(buf) + 1) * sizeof(char));
    //handle memory allocation error
    strcpy(suffix[i], buf);
}

首先,如一般评论所述,您通过使用相同的缓冲区作为 sscanf 的源输入和目标目标来调用未定义的行为。根据 C 标准,这是不允许的。

为此使用的正确函数可能是 strtok。下面是一个非常简单的例子。

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

int main()
{
    char line[] = ".txt;.ods;_music.mp3;.mjpeg;.ext1;.ext2";
    size_t slen = strlen(line); // worst case
    char *suffix[slen/2+1], *ext;
    size_t count=0;

    for (ext = strtok(line, ";"); ext; ext = strtok(NULL, ";"))
        suffix[count++] = ext;

    // show suffix array entries we pulled
    for (size_t i=0; i<count; ++i)
        printf("%s ", suffix[i]);
    fputc('\n', stdout);
}

输出

.txt .ods _music.mp3 .mjpeg .ext1 .ext2 

备注

  • 此代码假设最坏情况下的后缀数是字符串长度的一半,因此单个字符后缀列表在分隔符上拆分。
  • 后缀数组包含指向现在切片的原始行缓冲区的指针。因此,这些指针的可用性寿命仅与行缓冲区本身一样长。

希望对您有所帮助。