#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
/*
"APPID","人脸库","创建时间","用户组ID","修改时间","用户ID","修改时间","人脸url"
"25497897","人脸识别","2022-01-11 16:09:21","wbyq","2022-01-13 11:02:50","18679314703","2022-01-13 11:00:52","https://console.bce.baidu.com/ai/s/facelib/face?appId=3028051&groupId=wbyq&uid=18679514703&faceId=070f81ef00383d7c376aac1c38b73090"
"25497897","人脸识别","2022-01-11 16:09:21","wbyq","2022-01-13 11:02:50","18659514703","2022-01-13 11:00:52","https://console.bce.baidu.com/ai/s/facelib/face?appId=3028051&groupId=wbyq&uid=18679514703&faceId=f83fc8ca898f4d4a2a17ef4dc341187e"
"25497897","人脸识别","2022-01-11 16:09:21","wbyq","2022-01-13 11:02:50","18679514703","2022-01-13 11:00:52","https://console.bce.baidu.com/ai/s/facelib/face?appId=3028051&groupId=wbyq&uid=18679514703&faceId=65041fdb1f58fbd6c3b983045b8f840a"
"25497897","人脸识别","2022-01-11 16:09:21","wbyq","2022-01-13 11:02:50","18679214703","2022-01-13 11:00:52","https://console.bce.baidu.com/ai/s/facelib/face?appId=3028051&groupId=wbyq&uid=18679514703&faceId=39555691bf84a277bee85a2f1cd09e68"
"25497897","人脸识别","2022-01-11 16:09:21","wbyq","2022-01-13 11:02:50","18671514703","2022-01-13 11:00:52","https://console.bce.baidu.com/ai/s/facelib/face?appId=3028051&groupId=wbyq&uid=18679514703&faceId=0ff8d148721ae720c5815934929e77b3"
"25497897","人脸识别","2022-01-11 16:09:21","wbyq","2022-01-13 11:02:50","18679513703","2022-01-13 11:00:52","https://console.bce.baidu.com/ai/s/facelib/face?appId=3028051&groupId=wbyq&uid=18679514703&faceId=aebc792d6c5fec000be59a934dfd28c3"
*/
int readfile_csv(char *filename)
{
char c;
FILE *fp = fopen(filename,"rb");
int file_line=1,file_row=0;
fseek(fp,0,SEEK_END);
long flen = ftell(fp);
printf("len = %ld\n",flen);
fseek(fp,0,SEEK_SET);
int fd=fileno(fp);//文件指针转文件描述符
if(fd==-1)
{
printf("转换失败\n");
return -1;
}
char *file_addr=mmap(NULL,flen,PROT_READ,MAP_SHARED,fd,0);//内存映射
fclose(fp);
close(fd);
if(file_addr==NULL)
{
printf("映射失败\n");
return -2;
}
/*统计行*/
char *p=file_addr;
p=strstr(file_addr,"\"");
while(*p)
{
if(*p=='\n')
{
p=strstr(p,"\"");
if(p==NULL)break;
file_line++;
}
else p++;
}
printf("行:%d\n",file_line);//统计行
//"APPID","人脸库","创建时间","用户组ID","修改时间","用户ID","修改时间","人脸url"
/*统计列*/
int cnt=0;
p=file_addr;
p=strstr(file_addr,"\"");
p++;
while(*p && *p!='\n')
{
while(*p!='"' && *p)p++;
p++;
cnt++;
if(*p=='\n')break;
else if(*p==',')p+=2;
}
printf("列:%d\n",cnt);
/*将.csv文件数据保存到数组中*/
char buff[file_line][cnt][500];
int i,j;
p=strstr(file_addr,"\"");
p++;
for(i=0;i<file_line;i++)
{
for(j=0;j<cnt;j++)
{
int index=0;
while(*p!='"' && *p)
{
buff[i][j][index++]=*p;
p++;
}
p++;
buff[i][j][index++]='\0';
if(*p=='\n')
{
p=strstr(p,"\"");
p++;
}
else if(*p==',')p+=2;
}
}
munmap(file_addr,flen);/*取消映射*/
for(i=0;i<file_line;i++)
{
//printf("\n-------------------------------\n");
for(j=0;j<cnt;j++)
{
printf("%s",buff[i][j]);
}
printf("\n---------------------------------\n");
}
return 0;
}
int main()
{
readfile_csv("1.csv");
return 0;
}