gpt4 book ai didi

c - 从具有结构数组的 CSV 文件中读取

转载 作者:太空宇宙 更新时间:2023-11-04 04:42:55 25 4
gpt4 key购买 nike

我是 C 编程的新手,我想尝试读取 CSV 文件并将其打印出来。我的 CSV 文件格式为:

指数,国家,年份,死亡率

1个世界

         50s     36
60s 29
70s 22

2 非洲

         50s     34

209汤加

         50s     49
60s 67

等等……

我不确定如何读取我的 CSV 文件的末尾,因此我的代码输出最多只能打印索引 143,而我当前的 CSV 文件最多只能打印 204。有人可以指导我吗?

void readRecords(){
FILE *fptr;
int i,j,k;
char line[200], hold[10];

if((fptr=fopen("data.csv","r"))==NULL){
printf("Cannot open input file data.csv\n");
}

else{
for(i=0;i<999;i++){
fgets(line, 200, fptr);

j=0;
k=0;
while (line[j] != ','){ // This while loop extract the country name until a comma is detected
hold[k++] = line[j++]; // Simply copy all character from each line to the country
}
hold[k]='\0';
myRecords[i].index=atoi(hold);
j++;


k=0;
while (line[j] != ','){ // This while loop extract the country name until a comma is detected
myRecords[i].country[k++] = line[j++]; // Simply copy all character from each line to the country
}
myRecords[i].country[k]='\0';
j++;


k=0;
while (line[j] != ','){ // This while loop extract the country name until a comma is detected
hold[k++] = line[j++]; // Simply copy all character from each line to the country
}
hold[k]='\0';
myRecords[i].year=atoi(hold);
j++;


k=0;
while (line[j] != ','){ // This while loop extract the country name until a comma is detected
hold[k++] = line[j++]; // Simply copy all character from each line to the country
}
hold[k]='\0';
myRecords[i].deathrate=atoi(hold);
j++;

}

}
fclose(fptr);
}

最佳答案

另一种选择是简单地单步执行 fgetsgetline 使用指针读取的行,并将逗号替换为 \0 字符和随着您的工作,逐步读取每个变量。这是给定您的数据文件的有效实现:

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

int main (int argc, char *argv[]) {

if (argc < 2) {
fprintf (stderr, "error: insufficient input, usage: %s <filename>\n", argv[0]);
return 1;
}
FILE *fptr;
char *line = NULL;
size_t len = 0;
ssize_t read;
char *p = NULL;
char *start = NULL;

int index = 0;
char *country;
int years = 0;
int deaths = 0;

if ((fptr=fopen (argv[1],"r")) == NULL) {
fprintf(stderr, "Cannot open input file %s\n", argv[1]);
}

while ((read = getline(&line, &len, fptr)) != -1)
{
p = line;

while (*p != ',' && *p != 0) p++;
*p = '\0';
index = atoi (line);
start = ++p;
while (*p != ',' && *p != 0) p++;
*p = '\0';
country = strdup (start);
start = ++p;
while (*p != ',' && *p != 0) p++;
*p = '\0';
years = atoi (start);
start = ++p;
deaths = atoi (start);

printf ("idx: %3d country: %12s years: %3d deaths: %3d\n",
index, country, years, deaths);
}

free(line);

return 0;
}

现在,给定一个格式为 .csv 的输入文件:

1,world,50s,36
1,world,60s,65
1,world,70s,58
2,africa,50s,37
2,africa,60s,66
2,africa,70s,59
207,tonga,50s,37
207,tonga,60s,66
207,tonga,70s,59

您的输出将是:

idx:   1   country:        world  years:  50  deaths:  36
idx: 1 country: world years: 60 deaths: 65
idx: 1 country: world years: 70 deaths: 58
idx: 2 country: africa years: 50 deaths: 37
idx: 2 country: africa years: 60 deaths: 66
idx: 2 country: africa years: 70 deaths: 59
idx: 207 country: tonga years: 50 deaths: 37
idx: 207 country: tonga years: 60 deaths: 66
idx: 207 country: tonga years: 70 deaths: 59

这两种方式都差不多。使用 strtok 或只是单步执行缓冲区并直接读取您需要的内容。如果您花时间仔细阅读代码。你会看到它所做的一切都是读取直到遇到逗号,用 \0 替换逗号,从头(或开始)读取以获取所需的信息,将指针前进 1,设置新的“开始”点,然后一直重复缓冲区。读取“死亡”后,最后一个循环确保已读取该行中的所有字符。 getline 读取下一行并继续,直到无法在 .csv 文件中读取更多行。我希望这有帮助。 (如果不是很明显,您可以在命令行中为其提供 csv 文件名:

$ readcsv dat.csv

您已经拥有数据并将其解析为上面的变量。如果要使用结构,只需将存储类型从普通变量更改为结构数据类型即可。类似于以下的内容适用于结构数组。

int recs = 0;

struct _stats {
int index;
char *country;
int years;
int deaths;
};

struct _stats *stats[100];
struct _stats *statsptr = NULL;

if ((fptr=fopen (argv[1],"r")) == NULL) {
fprintf(stderr, "Cannot open input file %s\n", argv[1]);
}

while ((read = getline(&line, &len, fptr)) != -1) {

p = line;
statsptr = malloc (sizeof (struct _stats));

while (*p != ',' && *p != 0) p++;
*p = '\0';
statsptr->index = atoi (line);
start = ++p;

while (*p != ',' && *p != 0) p++;
*p = '\0';
statsptr->country = strdup (start);
start = ++p;

while (*p != ',' && *p != 0) p++;
*p = '\0';
statsptr->years = atoi (start);
start = ++p;

statsptr->deaths = atoi (start);

stats[recs] = statsptr;
recs++;

}

int iter = 0;
for (iter = 0; iter < recs; iter++) {
printf ("idx: %3d country: %12s years: %3d deaths: %3d\n",
stats[iter]->index,
stats[iter]->country,
stats[iter]->years,
stats[iter]->deaths);
}

除了最初声明结构指针的最大数量外,您还需要使用动态数据类型,如链表。 (超出本次讨论的范围)。

关于c - 从具有结构数组的 CSV 文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24115436/

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