C读取csv文件

时间:2012-08-22 17:49:35

标签: c parsing csv

我遇到了一个我以前没有遇到过的问题而且感到困惑...出于某种原因,当我尝试通过char读取一个CSV文件char时,似乎空格以某种方式被放置在那里......更奇怪的是,任何地方都不存在空间字符。我举一个例子......

char *readgd(const char *fname)
{
    char *gddata, *tmp;
    FILE *fp;
    int buff = 1024, c = 0, ch;

    if(!(fp = fopen(fname, "r")))
    {
        printf("\nError! Could not open %s!", fname);
        return 0x00;
    }
    if(!(gddata = malloc(buff)))
    {
        fclose(fp);
        printf("\nError! Memory allocation failed!");
        return 0x00;
    }
    while(ch != EOF)
    {
        c++;
        ch = fgetc(fp);
        if(buff <= c)
        {
            buff += buff;
            if(!(tmp = realloc(gddata, buff)))
            {
                free(gddata);
                fclose(fp);
                printf("\nError! Memory allocation failed!");
            }
            gddata = tmp;
        }
        gddata[c - 1] = ch;
        if(gddata[c - 1] != ' ') printf("%c", gddata[c - 1]); //no spaces?
    }
    if(!(tmp = realloc(gddata, c + 1)))
    {
        free(gddata);
        fclose(fp);
        printf("\nError! Memory allocation failed!");
    }
    gddata = tmp;
    gddata[c] = 0x00;
    fclose(fp);

    return gddata;
}

使用以下CSV代码段:

:Tagname,Area,SecurityGroup,Container,ContainedName,ShortDesc,ExecutionRelativeOrder,ExecutionRelatedObject,UDAs,Extensions,CmdData,Address_ACbHAlmCfg,Address_ACbHWarnCfg,Address_ACbLAlmCfg,Address_ACbLWarnCfg,Address_ACbTfCfg,Address_ACrHAlmDb,Address_ACrHAlmSp,Address_ACrHAlmTmrSp,Address_ACrHWarnDb,Address_ACrHWarnSp,Address_ACrHWarnTmrSp,Address_ACrLAlmDb,Address_ACrLAlmSp,Address_ACrLAlmTmrSp,Address_ACrLWarnDb,Address_ACrLWarnSp,Address_ACrLWarnTmrSp,Address_ACrTfTmrSp,Address_bHalm,Address_bHWarn,Address_bLAlm,Address_bLwarn,Address_bMode,Address_bTfAlm,Address_rCCmd,Address_rVal,

将此输出到控制台:

■: T a g n a m e , A r e a , S e c u r i t y G r o u p , C o n t a i n e r , C
    o n t a i n e d N a m e , S h o r t D e s c , E x e c u t i o n R e l a t i v e
    O r d e r , E x e c u t i o n R e l a t e d O b j e c t , U D A s , E x t e n s
    i o n s , C m d D a t a , A d d r e s s _ A C b H A l m C f g , A d d r e s s _
    A C b H W a r n C f g , A d d r e s s _ A C b L A l m C f g , A d d r e s s _ A
    C b L W a r n C f g , A d d r e s s _ A C b T f C f g , A d d r e s s _ A C r H
    A l m D b , A d d r e s s _ A C r H A l m S p , A d d r e s s _ A C r H A l m T
    m r S p , A d d r e s s _ A C r H W a r n D b , A d d r e s s _ A C r H W a r n
    S p , A d d r e s s _ A C r H W a r n T m r S p , A d d r e s s _ A C r L A l m
    D b , A d d r e s s _ A C r L A l m S p , A d d r e s s _ A C r L A l m T m r S
    p , A d d r e s s _ A C r L W a r n D b , A d d r e s s _ A C r L W a r n S p ,
    A d d r e s s _ A C r L W a r n T m r S p , A d d r e s s _ A C r T f T m r S p
    , A d d r e s s _ b H a l m , A d d r e s s _ b H W a r n , A d d r e s s _ b L
    A l m , A d d r e s s _ b L w a r n , A d d r e s s _ b M o d e , A d d r e s s
    _ b T f A l m , A d d r e s s _ r C C m d , A d d r e s s _ r V a l ,

我对这些空间的来源感到非常困惑。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:5)

您确定CSV未使用UTF-16编码(每个字符使用两个字节)吗?

这是您在其他有效ASCII字符之间看到空格的最可能原因,因此请先尝试验证编码。