简体   繁体   English

Strsep,进一步解析CSV输入

[英]Strsep, Parsing CSV Input further

Using strsep to split a CSV with a bunch of usless junk in it ("," Delim). 使用strsep拆分CSV,其中包含一堆无用的垃圾(“,” Delim)。 One of the entries has quotes on either side (ie Florida,"Bob",1999) and I'd like to pull those out before I save them in my array. 其中一个条目的两边都带有引号(例如,Florida,“ Bob”,1999年),我想先将其引出,然后再将其保存在数组中。

How do I remove the quotes from the name? 如何删除名称中的引号? Thanks! 谢谢!

for (int i = 0; i < 19; i++)
{                               
  token = strsep(&copy, ","); //Split token
  if (i ==  3) {one_spot->location = token;}
  if (i == 17) {one_spot->name = token;}//the Name is in quotes in CSV
  if (i == 18) {one_spot->year = atoi(token);}
}
all_spots[j] = one_spot; //Add to array.

You could do something like this: 您可以执行以下操作:

  1. look for the first " using strchr 查找第一个"使用strchr
  2. If found, look for the next " 如果找到,请寻找下一个"
  3. Use memcpy to copy the strings between the quotes. 使用memcpy在引号之间复制字符串。

if (i == 17)
{
    char *firstq = strchr(token, '"');
    if(firstq == NULL)
    {
        one_song->name = strdup(token);
        continue;
    }

    char *lastq = strchr(firstq++, '"');
    if(lastq == NULL)
    {
        // does not end in ", copy everything
        one_song->name = strdup(token);
        continue;
    }

    size_t len = lastq - firstq;

    char *word = calloc(len + 1, 1);
    if(word == NULL)
    {
        // error handling, do not continue
    }

    memcpy(word, firstq, len); // do not worry about \0 because of calloc
    one_song->name = word;
}

Note that I use strdup to do the assignment one_song->name = strdup(token); 请注意,我使用strdup进行了分配one_song->name = strdup(token); and calloc to allocate memory. calloc分配内存。 strsep returns a pointer to copy + an offset. strsep返回要copy的指针+偏移量。 Depending how you created/allocated copy , this memory might be invalid once the function exits. 取决于您创建/分配copy ,一旦函数退出,此内存可能无效。 That's why it's better to create a copy of the original before you assign it to the struct. 这就是为什么在将原始副本分配给该结构之前最好创建一个副本。

This code is very simple, it does not handle spaces at the beginning and end of the string. 该代码非常简单,它不处理字符串开头和结尾的空格。 It can distinguish between abc and "abc" but it fails at "abc"d or "abc"def" . It also does not handle escaped quotes, etc. This code shows you only a way of extracting a string from the quotes. It's not my job to write your exercise for you, but I can show you how to start. 它可以区分abc"abc"但在"abc"d"abc"def"时失败。它也不处理转义的引号等。此代码仅向您显示一种从引号中提取字符串的方法。为您编写练习不是我的工作,但我可以向您展示如何开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM