我是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);
}
答案 0 :(得分:0)
另一种选择是简单地使用指针逐步读取fgets
或getline
读取的行,并将逗号替换为\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);
}
除了最初声明结构的最大指针数之外,您还需要使用动态数据类型,如链接列表。 (超出了本讨论的范围)。