#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_SIZE 1024
int main() {
// 打开要读取的CSV文件
FILE *csvFile = fopen("data.csv", "r");
if (csvFile == NULL) {
perror("Failed to open file");
return 1;
}
char line[MAX_LINE_SIZE];
// 逐行读取CSV文件
while (fgets(line, sizeof(line), csvFile) != NULL) {
// 使用逗号分隔符解析数据
char *name = strtok(line, ",");
char *ageStr = strtok(NULL, ",");
char *city = strtok(NULL, "\n");
// 将字符串转换为相应的数据类型
int age = atoi(ageStr);
// 输出读取的数据
printf("Name: %s, Age: %d, City: %s\n", name, age, city);
}
// 关闭文件
fclose(csvFile);
return 0;
}
标签:NULL,读取,strtok,csvFile,C语言,char,line,csv From: https://www.cnblogs.com/yuyanc/p/17814481.html