简体   繁体   中英

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. 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:

: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)?

This is the most likely reason you'd see spaces between otherwise valid ASCII characters, so try verifying the encoding first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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