逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样被解读的数据。CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗号或制表符。通常,所有记录都有完全相同的字段序列。通常都是纯文本文件。建议使用WORDPAD或是记事本来开启,再则先另存新档后用EXCEL开启,也是方法之一。
CSV文件格式的通用标准并不存在,但是在RFC 4180中有基础性的描述。使用的字符编码同样没有被指定,但是bitASCII是最基本的通用编码。
1.通过excel打开文件
2.通过Notepad++打开或记事本打开
#include
#include
#include
#include
#include
/*
"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
运行结果:
4.修改CSV文件
/*
函数功能:修改csv文件
形参:filename --文件名
line --要修改的行(从1开始)
row --要修改的列(从1开始)
data --修改的内容
返回值:成功返回0,失败返回负数
*/
int modifyfile_csv(char *filename,int line,int row,char *data)
{
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;ifile_line || row>cnt)
{
printf("未找到要修改的位置\n");
return 0;
}
char src_data[500];
if(row==cnt)
{
snprintf(src_data,sizeof(src_data),"\"%s\"\n",data);
}
else snprintf(src_data,sizeof(src_data),"\"%s\",",data);
strcpy(buff2[line-1][row-1],src_data);
int size;
for(i=0;i
运行结果:
全部0条评论
快来发表一下你的评论吧 !