我是一个C编程新手,我想尝试从CSV文件中读取并打印出来。我的CSV文件的格式为:
指数、国家、年份、死亡率
1个世界
50s 36
60s 29
70s 222非洲
50s 34209汤加
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);
}发布于 2014-06-09 16:48:50
另一种选择是用指针简单地遍历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,设置新的'start‘点,然后一直向下重复缓冲区。在读取'deaths‘之后,最后一个循环确保行中的所有字符都已被读取。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);
}除了最初声明指向结构的最大数量的指针之外,您还需要使用动态数据类型,如链表。(超出了本讨论的范围)。
https://stackoverflow.com/questions/24115436
复制相似问题