当某些字段为空时,如何使用 sscanf 和 scanset 提取逗号分隔字符串中的字段 [英] How to extract fields in comma separated string using sscanf and scanset when some fields blank

查看:45
本文介绍了当某些字段为空时,如何使用 sscanf 和 scanset 提取逗号分隔字符串中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果所有字段都已填充,我可以使用 sscanf(见下文)以逗号分隔格式提取字段.但是如果一个字段是空白的,那么只会填充到空白字段.有什么办法可以继续,忽略空白字段的问题,以便填充后续字段?

I am able to extract fields in comma separate format using sscanf (see below) if all fields are populated. But if a field is blank then only up to the blank field is populated. Is there any way I can carry on, ignoring problem of blank fields so that subsequent fields do get populated?

#include <stdio.h>

int main(int argc, char* argv[]) {

   char* s = "Apple,Pear,Potato,11";

   char fruit1[10];
   char fruit2[10];
   char vegetable[10];
   int size;


   int num = sscanf(s, "%20[^,],%20[^,],%20[^,],%d", fruit1, fruit2, vegetable, &size);
   if (num == 4) {
      printf("This record contains 4 items\n");
      printf("first fruit: %s, second fruit: %s, vegetable: %s, size = %d\n", fruit1, fruit2, vegetable, size);
   }
   else {
      printf("This record does not contain 4 items\n");
   }


   // but here it fails - blank vegetable
   char* t = "Plum,Orange,,12";
   num = sscanf(t, "%20[^,],%20[^,],%20[^,],%d", fruit1, fruit2, vegetable, &size);

   if (num == 4) {
      printf("This record contains 4 items\n");
      printf("first fruit: %s, second fruit: %s, vegetable: %s, size = %d\n", fruit1, fruit2, vegetable, size);
   }
   else {
      printf("This record does not contain 4 items\n");
   }

   return 0;
}


/*
Prints:
This record contains 4 items
first fruit: Apple, second fruit: Pear, vegetable: Potato, size = 11
This record does not contain 4 items
*/

推荐答案

您可以使用 strchr 构建自己的扫描功能,注意 scan 替换了 与原始字符串中的 \0 ,所以如果你想标记一个字符串文字(只读),你需要一个中间缓冲区:

You can build your own scan function using strchr, note that scan replaces , with \0 in the original string, so if you want to tokenize a string literal (read only) you need an intermediate buffer:

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

char *scan(char **pp, char c)
{
    char *s = *pp, *p;

    p = strchr(*pp, c);
    if (p) *p++ = '\0';
    *pp = p;
    return s;
}

int main(void)
{
    char s1[] = "Apple,Pear,Potato,11";
    char s2[] = "Plum,Orange,,12";
    char fruit1[10];
    char fruit2[10];
    char vegetable[10];
    int size;
    char *p;

    p = s1;
    strcpy(fruit1, scan(&p, ','));
    strcpy(fruit2, scan(&p, ','));
    strcpy(vegetable, scan(&p, ','));
    size = strtol(scan(&p, ','), NULL, 10);
    printf("first fruit: %s, second fruit: %s, vegetable: %s, size = %d\n", fruit1, fruit2, vegetable, size);

    p = s2;
    strcpy(fruit1, scan(&p, ','));
    strcpy(fruit2, scan(&p, ','));
    strcpy(vegetable, scan(&p, ','));
    size = strtol(scan(&p, ','), NULL, 10);
    printf("first fruit: %s, second fruit: %s, vegetable: %s, size = %d\n", fruit1, fruit2, vegetable, size);

    return 0;
}

您可以使用指针简化代码:

You can simplify your code using pointers:

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

char *scan(char **pp, char c)
{
    char *s = *pp, *p;

    p = strchr(*pp, c);
    if (p) *p++ = '\0';
    *pp = p;
    return s;
}

int main(void)
{
    char s1[] = "Apple,Pear,Potato,11";
    char s2[] = "Plum,Orange,,12";
    char *v[4], *p;
    int i;

    p = s1;
    for (i = 0; i < 4; i++) v[i] = scan(&p, ',');
    printf("first fruit: %s, second fruit: %s, vegetable: %s, size = %d\n", v[0], v[1], v[2], atoi(v[3]));

    p = s2;
    for (i = 0; i < 4; i++) v[i] = scan(&p, ',');
    printf("first fruit: %s, second fruit: %s, vegetable: %s, size = %d\n", v[0], v[1], v[2], atoi(v[3]));

    return 0;
}

这篇关于当某些字段为空时,如何使用 sscanf 和 scanset 提取逗号分隔字符串中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆