c语言实现读取csv文件,并对数据进行分析。

总代码:

```c在这里插入代码片
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student
{
char shuzu[20];

}students[8][21];
int main()
{
int i=0,k=0,q=0;
FILE *fp = NULL;

char *line,*record;

char buffer[1024];
float a[8][21]={0};
if ((fp = fopen(“C:\Users\throb\Desktop\温度.csv”, “at+”)) != NULL)
{
fseek(fp, 0L, SEEK_SET); //定位到第二行,每个英文字符大小为1
char delims[] = “,”;
char *result = NULL;
int j = 0;
while ((line = fgets(buffer, sizeof(buffer), fp))!=NULL)//当没有读取到文件末尾时循环继续
{
record = strtok(line, “,”);

while (record != NULL)//读取每一行的数据
{

printf("%s ", record);//将读取到的每一个数据打印出来
strcpy(students[i][k].shuzu,record);
k++;
record = strtok(NULL, “,”);
j++;

}
i++;
k=0;
printf("\n");
j = 0;
}
fclose(fp);
fp = NULL;
}

for(i=0;i<8;i++)
{
for(k=0;k<21;k++)
{
a[i][k]=atof(students[i][k].shuzu);
}
}
printf("\n");
printf("=============\n");
for(i=2;i<8;i++)
{
for(k=1;k<21;k++)
{
if(a[i][k]>37.0)
{
printf("%s\n",students[i][0].shuzu);
printf("%s\n",students[0][k].shuzu);
printf("%s\n",students[i][k].shuzu);
q++;

}                 

}
}
printf("=============\n");
printf(“人数%d人”,q);
}

猜你喜欢

转载自blog.csdn.net/weixin_45663946/article/details/107426288
今日推荐