简体   繁体   English

将'C'中的char数组拆分为CSV

[英]Split char array in 'C' to CSV's

I need to split a char array into CSV's. 我需要将一个char数组拆分为CSV。 Actually we can do the reverse of it using strtok() like: 实际上,我们可以使用strtok()进行相反的操作:

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

int main ()
{
 char str[] ="This,a,sample,string.";
 char * pch;
 printf ("Splitting string \"%s\" into tokens:\n",str);
 pch = strtok (str,",");
 while (pch != NULL)
 {
   printf ("%s\n",pch);
   pch = strtok (NULL, ",");
 }
 return 0;
}

But in my case, there's an char array suppose char bits[1024]="abcdefghijklmn" . 但在我的情况下,有一个char数组,假设char bits[1024]="abcdefghijklmn" I need to get the output as a,b,c,d,e,f,g,h,i,j,k,m,n . 我需要将输出获取为a,b,c,d,e,f,g,h,i,j,k,m,n

Is there any function or library to do this ie in terms of raw meaning, for every character it has to put a comma. 是否有任何功能或库可以执行此操作,即就原始含义而言,必须为每个字符加上逗号。

Just iterate over the string until you hit the end-of-string '\\0' character. 只需遍历字符串,直到您击中字符串结尾'\\0'为止。 Or use the length of the data in the array (which may be smaller than the array size) and use a simple for loop. 或使用数组中的数据长度(可能小于数组大小)并使用简单的for循环。

you can use this simple function from old basic : 您可以从旧的基础开始使用此简单功能:

// ............................................................. string word at

char * word_at(char *tString, int upTo, char *dilim) {

     int wcount;
     char *rString, *temp;
    temp= (char *) malloc(sizeof(char) * (strlen(tString)+1));
    strcpy(temp, tString);

    rString= strtok(temp, dilim);
    wcount=1;

    while (rString != NULL){
        if (wcount==upTo) { 
     return rString;
        }
        rString= strtok(NULL, dilim);
        wcount++;
    }
   return tString ;
  }

parameter : string , index and character delimiter return : word : ( char *) 参数:字符串,索引和字符定界符返回:单词:(char *)

This works for a null terminated string. 这适用于以空终止的字符串。 But it will leave a dangling comma at the end. 但这最后会留下一个悬空的逗号。

void tokenise(char *s, char *d)
{
 while(*d++ = *s++) *d++ = ',';
}

If you know the length of the string already, you can pass that through. 如果您已经知道字符串的长度,则可以通过它。 This will not leave a dangling comma. 这不会留下悬挂的逗号。

void tokenise(char *s, char *d, int length)
{
    int i = 0;
    while((*d++ = *s++) && ((i++)<(length-1))) *d++ = ',';
}

In both examples, s is a pointer to the source string and d points to the output tokenised string. 在两个示例中, s是指向源字符串的指针,而d指向输出标记化的字符串。 It is up to the calling code to ensure the buffer d points to is sufficiently large. 由调用代码来确保缓冲区d指向足够大。

If you find easy to implement it, then this could help you to start 如果您发现易于实施,则可以帮助您开始

char* split_all( char arr[], char ch )
{
    char *new, *ptr;

    new = ptr = calloc( 1, 2*strlen( arr ) ); // FIXME : Error checks

    for( ; *(arr + 1) ; new++, arr++ )
    {
            *new = *arr;
            new++;
            *new = ch;
    }

    *new = *arr;
    return ptr;
}

You can re-use, optimize this for your requirement. 您可以重新使用,并根据需要对其进行优化。 Its a quick and dirty solution, feel free to fix it.. 它是一种快速而肮脏的解决方案,请随时对其进行修复。

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

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