我需要从CSV文件中读取一些数据并创建一个数据结构,以便创建一个哈希表。我尝试了很多,损失了很多时间,但都没有结果。请检查我的代码并帮助我找出错误所在。。。
以下是文件数据、输出和代码。
(代码是我一个人写的,在Deitel的C编程语言和Deitel书的帮助下。)
代码
#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%10sn","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.2fn",
client.acctNum,client.breaker,client.datetime,client.current );
}
}
fclose ( cfPtr );
}
return 0 ;
}
CSV输入文件
acct,breaker,S_TimeStamp,MeasurementValue1,EthitideTalantiMVBreakerBB2Q2Current,1/12/2015 8:30,37.62,EthitideTalantiMVBreakerBB2Q2Current,1/12/2015 9:00,34.23,EthitideTalantiMVBreakerBB2Q2Current,1/12/2015 9:30,37.24,EthitideTalantiMVBreakerBB2Q2Current,1/12/2015 10:00,40.65,EthitideTalantiMVBreakerBB2Q2Current,1/12/2015 10:30,41.86,Eth1itideTalantiMVBreakerBB2Q2Current,1/12/2015 11:00,45.8
输出
账户姓氏名字余额1952670561,断路器,S_TimeStamp,MeasurementValue1,EthitideTalantiMVBreakerBB2Q2Current,1/12/2015 8:30,37.62.EthiotideAtalantiMVBreak@talantiMVBreakerBB2Q2Current,2015年1月12日8:30,37.62.EthiotideAtalantiMVBreak@178724158464812467496986462827554460798109233540662154481413250685702096704425934569739618465990294960631478737616585889239313529311067888348744695823558167564353119398296863451093505908419875899425197001801728.0011116509172Q2当前,1/12/2015 9:00,34.23,EthitideTalantiMVBreakerBB2Q2Current,1/12/2015 9:30,37.24.EthiotideAtalantiMVBreakerBB2Q2C@VBreakerBB2Q2Current,2015年1月12日9:30,37.24.EthiotideAtalantiMVBreakerBB2Q2C@5155825882657381.001701999221 nt,1/12/2015 10:00,40.65,EthitideTalantiMVBreakerBB2Q2Current,1/12/2015 10:30,41.86.Eth1iotideAtalantiMVBreakerBB2Q2Curren@rBB2Q2Current,2015年1月12日10:30,41.86.Eth1iotideAtalantiMVBreakerBB2Q2Curren@62020397019943253271215041932658580009235443368333447944639125867547040589859590642175830919754237423637956114804980964218473105282339526865630710982971497957257635386734871861549462238776468516448931338295812955819267325952.0079175179612/2015 11:00,45.840.6 5,EthitideTalantiMVBreakerBB2Q2Current,1/12/2015 10:30,41.8 6,Eth1iotideAtalantiMVBreakerBB2Q2Curren@rBB2Q2Current,2015年1月12日10:30,41.86.Eth1iotideAtalantiMVBreakerBB2Q2Curren@62020397019943253271215041932658580009235443368333447944639125867547040589859590642175830919754237423637956114804980964218473105282339526865630710982971497957257635386734871861549462238776468516448931338295812955819267325952.00
#include <stdio.h>
struct clientData{
int acctNum;
char breaker[50];
char datetime[60];
double current;
};
int main(void){
FILE *cfPtr;
struct clientData client = {0,"","",0.0 };
if((cfPtr = fopen("data1.csv","r")) == NULL){
printf("File could not be opened.n");
} else {
char line[96];
printf("%-6s%-40s%-20s%10sn", "Acct", "Breaker", "TimeStamp", "MeasurementValue");
while(fgets(line, sizeof line, cfPtr)){
int state = sscanf(line, "%d,%49[^,],%59[^,],%lf", &client.acctNum, client.breaker, client.datetime, &client.current);
if(state == 4 ) {
printf("%-6d%-40s%-20s%10.2fn",
client.acctNum, client.breaker, client.datetime, client.current);
}
}
fclose ( cfPtr );
}
return 0;
}