简体   繁体   English

C读取csv文件

[英]C reading csv file

I'm running into a problem I haven't encountered before and am baffled... for some reason when I try to read a CSV file char by char but it seems like spaces are somehow getting placed there... and what's weirder is the fact that no space chars exist anywhere. 我遇到了一个我之前从未遇到过的问题,感到困惑……由于某些原因,当我尝试按char读取CSV文件char时,但似乎空格以某种方式被放置在那里……而奇怪的是任何地方都不存在空格的事实。 I will give an example... 我举个例子...

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;
}

with the following CSV snippet: 包含以下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,

outputs this onto the console: 将其输出到控制台:

■: 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 ,

I am very confused as to where these spaces are coming from. 对于这些空间的来源,我感到很困惑。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Are you sure the CSV is not encoded with UTF-16 (using two bytes per character)? 您确定CSV未使用UTF-16编码(每个字符使用两个字节)吗?

This is the most likely reason you'd see spaces between otherwise valid ASCII characters, so try verifying the encoding first. 这是最有可能的原因,您会在其他有效的ASCII字符之间看到空格,因此请首先尝试验证编码。

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

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