samedi 27 juin 2015

CSV file data in structure data for hashing with C

I need to read some data from a CSV file and create a data structure, so that I create a hashtable. I have tried a lot and lost a lot of hours but without a result. Please check my code and help me find where the mistake is ...

Here are the file data, output and code.

Thanks in advance!! (I wrote the code alone, with help from C programming language of Deitel and Deitel book.)

Code

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>
#include <ctype.h>

struct clientData
{
   int acctNum; 

   char breaker[50]; 
   char datetime[60];
   double current;
 };

int main()
{ 
  FILE *cfPtr;

  struct clientData client = {0,"","",0.0 };
  if ( ( cfPtr = fopen("data1.csv","rb") ) == NULL)
  {
    printf("File could not be opened.\n");
  }   
  else
  {
    printf("%-6s%-16s%-11s%10s\n","Acct","Last Name","First Name","Balance");

    while ( !feof( cfPtr) )
    {
        fread( &client , sizeof ( struct clientData ), 1 , cfPtr );

        if ( client.acctNum !=0 )
        {
            printf ("%-6d%-16s%-11s%10.2f\n",
                    client.acctNum,client.breaker,client.datetime,client.current );
        }
    }
    fclose ( cfPtr );
  }
  return 0 ;
}

CSV Input file

acct,breaker,S_TimeStamp,MeasurementValue 1,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/20158:30,37.6 2,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/20159:00,34.2 3,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/20159:30,37.2 4,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/201510:00,40.6 5,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/201510:30,41.8 6,Eth1iotideAtalantiMVBreakerBB2Q2Current,1/12/201511:00,45.8

Output

Acct Last Name First Name Balance 1952670561,breaker,S_TimeStamp,MeasurementValue 1,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/20158:30,37.6 2,EthiotideAtalantiMVBreak@talantiMVBreakerBB2Q2Current,1/12/20158:30,37.6 2,EthiotideAtalantiMVBreak@178724158464812467496986462827554460798109233540662154481413250685702096704425934569739618465990294960631478737616585889239313529311067888348744695823558167564353119398296863451093505908419875899425197001801728.00 11116509172Q2Current,1/12/20159:00,34.2 3,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/20159:30,37.2 4,EthiotideAtalantiMVBreakerBB2Q2C@VBreakerBB2Q2Current,1/12/20159:30,37.2 4,EthiotideAtalantiMVBreakerBB2Q2C@5155825882657381.00 1701999221nt,1/12/201510:00,40.6 5,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/201510:30,41.8 6,Eth1iotideAtalantiMVBreakerBB2Q2Curren@rBB2Q2Current,1/12/201510:30,41.8 6,Eth1iotideAtalantiMVBreakerBB2Q2Curren@62020397019943253271215041932658580009235443368333447944639125867547040589859590642175830919754237423637956114804980964218473105282339526865630710982971497957257635386734871861549462238776468516448931338295812955819267325952.00 79175179612/201511:00,45.8 40.6 5,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/201510:30,41.8 6,Eth1iotideAtalantiMVBreakerBB2Q2Curren@rBB2Q2Current,1/12/201510:30,41.8 6,Eth1iotideAtalantiMVBreakerBB2Q2Curren@62020397019943253271215041932658580009235443368333447944639125867547040589859590642175830919754237423637956114804980964218473105282339526865630710982971497957257635386734871861549462238776468516448931338295812955819267325952.00

Aucun commentaire:

Enregistrer un commentaire