C语言解析CSV文件!

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

#define N 9
typedef struct a{
        char *p[N];
        int num;
}A;
A *split_csv(char *str)
{
        char buffer[128];
        int bwq  = 0; //begin with quote
        int len = 0,i = 0,j = 0;
        char ch;
        char *result;
        A *a;

        len = strlen(str);

        if(str == NULL ||  0 == len )
                return NULL;
        memset(buffer,'/0',sizeof(buffer));
        a = (A *)calloc(1,sizeof(A));
        for(i=0;i<len;i++)
        {
                if(j>=N)
                        break;
                ch = str[i];
                if(ch == '/"')
                {
                        if(bwq)
                        {
                                i++;
                                if(i>= len)
                                {
                                        result = calloc(1,128);
                                        sprintf(result,"%s",buffer);
                                        a->p[j++] = result;
                                        result = NULL;
                                        bzero(buffer,128);
                                        bwq=0;
                                }
                                else
                                {
                                    ch=str[i];
                                    if (ch == '/"')
                                    {
                                        sprintf(buffer,"%s%c",buffer,ch);
                                    }
                                    else if (ch == ',')
                                    {
                                                result = calloc(1,128);
                                        sprintf(result,"%s",buffer);
                                        a->p[j++] = result;
                                        result = NULL;
                                        bzero(buffer,128);
                                        bwq=0;
                                    }
                                    else
                                    {
                                        return NULL;
                                    }
                                }
                        }
                        else if(result == NULL ||0 == strlen(result))
                        {
                                bwq = 1;
                        }
                        else
                        {
                                return NULL;
                        }
                }
                else if(ch == ',')
                {
                        if(bwq)
                        {
                                sprintf(buffer,"%s%c",buffer,ch);
                        }
                        else
                        {
                                        result = calloc(1,128);
                                sprintf(result,"%s",buffer);
                                a->p[j++] = result;
                                result = NULL;
                                bzero(buffer,128);
                                bwq=0;
                        }
                }
                else
                {
                        sprintf(buffer,"%s%c",buffer,ch);
                }
        }
        if(j<N)
        {
                result = calloc(1,128);
                sprintf(result,"%s",buffer);
                a->p[j++] = result;
                result = NULL;
        }
        a->num = j;
        return a;
}
void free_a(A *a)
{
        int i = 0;
        if(NULL == a)
                return;
        for(i = 0 ;i< a->num; i++)
                free(a->p[i]);
        free(a);
        return ;
}
int main(void)
{
        char str[1024];
        A *a;
        int i=0;
        time_t begin;
        memset(str,'/0',1024);
        sprintf(str,"%s","/"e,/"/"eee/",ddd,aaaa,bbb,ccc,111,222,333,4444");
        a = split_csv(str);
        if(a == NULL)
        {
                printf("NULL/n");
                exit(0);
        }
        for(i = 0;i< a->num;i++)
                printf("%s/n",a->p[i]);
        free_a(a);
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值