gpt4 book ai didi

结构数据中的 CSV 文件数据,用于使用 C 进行哈希处理

转载 作者:行者123 更新时间:2023-11-30 20:37:59 25 4
gpt4 key购买 nike

我需要从 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%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 输入文件

acct,breaker,S_TimeStamp,MeasurementValue1,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/20158:30,37.62,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/20159:00,34.23,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/20159:30,37.24,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/201510:00,40.65,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/201510:30,41.86,Eth1iotideAtalantiMVBreakerBB2Q2Current,1/12/201511:00,45.8

输出

Acct Last Name First Name Balance1952670561,breaker,S_TimeStamp,MeasurementValue1,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/20158:30,37.62,EthiotideAtalantiMVBreak@talantiMVBreakerBB2Q2Current,1/12/20158:30,37.62,EthiotideAtalantiMVBreak@178724158464812467496986462827554460798109233540662154481413250685702096704425934569739618465990294960631478737616585889239313529311067888348744695823558167564353119398296863451093505908419875899425197001801728.00 11116509172Q2Current,1/12/20159:00,34.23,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/20159:30,37.24,EthiotideAtalantiMVBreakerBB2Q2C@VBreakerBB2Q2Current,1/12/20159:30,37.24,EthiotideAtalantiMVBreakerBB2Q2C@5155825882657381.001701999221nt,1/12/201510:00,40.65,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/201510:30,41.86,Eth1iotideAtalantiMVBreakerBB2Q2Curren@rBB2Q2Current,1/12/201510:30,41.86,Eth1iotideAtalantiMVBreakerBB2Q2Curren@62020397019943253271215041932658580009235443368333447944639125867547040589859590642175830919754237423637956114804980964218473105282339526865630710982971497957257635386734871861549462238776468516448931338295812955819267325952.00 79175179612/201511:00,45.840.6 5,EthiotideAtalantiMVBreakerBB2Q2Current,1/12/201510:30,41.8 6,Eth1iotideAtalantiMVBreakerBB2Q2Curren@rBB2Q2Current,1/12/201510: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%10s\n", "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.2f\n",
client.acctNum, client.breaker, client.datetime, client.current);
}
}
fclose ( cfPtr );
}
return 0;
}

关于结构数据中的 CSV 文件数据,用于使用 C 进行哈希处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31088499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com