Click here to Skip to main content
15,911,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,
i have a senario where i am suppose to read the data from the CSV file(C,C++),and need to store

below is example

444444,TESCO,3WHEEL,SONY,,HIGH ( this is one record of a csv file)
i am using below code to store all the value, but not abe to store ",,"

C++
void GetColumnValue( const char *lc_Record, char *Delim, char *pColmnName, long *FldCnt )
 {

     char Record[256] = {'\0'};
     char ItemCd[16]  = {'\0'};
     char AttrVal[21] = {'\0'};<code></code>
     char CompStr[1000] = {'\0'};

     long ll_FldCnt = 0;
     long ll_AttrNo = 0;


     strcpy(Record,lc_Record);
     char* p = strtok( Record, Delim );
     while( p != NULL )
     {
        if(ll_FldCnt == 0)
        {
            strcpy(ItemCd,p);
            strcat(ItemCd,",");
            //fprintf(pRes,"%s",ItemCd);
        }
        else
        {
            if( strlen(p) == 0 )
            {
                sprintf(gc_log, "There is no AttrValue in Request File For Itemcode %s and attrno %s",ItemCd, pConfigFile[ll_FldCnt].AttrNo );
                LogInfo(gc_log);
                continue;
            }
            strcpy(AttrVal,p);

            TrimString( AttrVal, '\n',2 );

 strcpy( CompStr, ItemCd );
            strcat( CompStr, pConfigFile[ll_FldCnt].AttrNo );
            strcat( CompStr,"," );
            strcat( CompStr, AttrVal );
            strcat( CompStr,"," );
            strcat( CompStr, gc_sysDt );
            strcat( CompStr,"," );
            strcat( CompStr, g_user_name );
            strcat( CompStr,"," );
            strcat( CompStr, gc_sysDt );
            strcat( CompStr,"," );
            strcat( CompStr, g_user_name );        }
        p=strtok( NULL,Delim);
        if( ll_FldCnt != 0 )
            fprintf( gp_RespFile,"%s\n",CompStr);
        ll_FldCnt++;
        strcpy( CompStr, "");

     }
     //fclose( gp_RespFile );
 }

please let me know how to fix it

Kind regards,
praveer

[Edit]Adding Tag in proper way- johny[/Edit]
Posted
Updated 30-Jan-12 16:28pm
v2

you could check if two consecutive commas(,) are present in string , then you can decide it right ?
 
Share this answer
 
Your code shows a message if there's no data in an entry. Is this code not working ? If it is working, what exactly is your issue ? Did you write this code ? Do you understand it ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900