1. csv文件簡介
逗號(hào)分隔值(Comma-Separated Values,CSV,有時(shí)也稱為字符分隔值,因?yàn)榉指糇址部梢圆皇嵌禾?hào)),其文件以純文本形式存儲(chǔ)表格數(shù)據(jù)(數(shù)字和文本)。純文本意味著該文件是一個(gè)字符序列,不含必須像二進(jìn)制數(shù)字那樣被解讀的數(shù)據(jù)。CSV文件由任意數(shù)目的記錄組成,記錄間以某種換行符分隔;每條記錄由字段組成,字段間的分隔符是其它字符或字符串,最常見的是逗號(hào)或制表符。通常,所有記錄都有完全相同的字段序列。通常都是純文本文件。建議使用WORDPAD或是記事本來開啟,再則先另存新檔后用EXCEL開啟,也是方法之一。
CSV文件格式的通用標(biāo)準(zhǔn)并不存在,但是在RFC 4180中有基礎(chǔ)性的描述。使用的字符編碼同樣沒有被指定,但是bitASCII是最基本的通用編碼。
2.csv文件信息
?1.通過excel打開文件
?2.通過Notepad++打開或記事本打開
3.讀取csv文件示例
#include
#include
#include
#include
#include
/*
"APPID","人臉庫","創(chuàng)建時(shí)間","用戶組ID","修改時(shí)間","用戶ID","修改時(shí)間","人臉url"
"25497897","人臉識(shí)別","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","人臉識(shí)別","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","人臉識(shí)別","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","人臉識(shí)別","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","人臉識(shí)別","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","人臉識(shí)別","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);//文件指針轉(zhuǎn)文件描述符
if(fd==-1)
{
printf("轉(zhuǎn)換失敗\n");
return -1;
}
char *file_addr=mmap(NULL,flen,PROT_READ,MAP_SHARED,fd,0);//內(nèi)存映射
fclose(fp);
close(fd);
if(file_addr==NULL)
{
printf("映射失敗\n");
return -2;
}
/*統(tǒng)計(jì)行*/
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);//統(tǒng)計(jì)行
//"APPID","人臉庫","創(chuàng)建時(shí)間","用戶組ID","修改時(shí)間","用戶ID","修改時(shí)間","人臉url"
/*統(tǒng)計(jì)列*/
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文件數(shù)據(jù)保存到數(shù)組中*/
char buff[file_line][cnt][500];
int i,j;
p=strstr(file_addr,""");
p++;
for(i=0;i
運(yùn)行結(jié)果:
4.修改CSV文件
/*
函數(shù)功能:修改csv文件
形參:filename --文件名
line --要修改的行(從1開始)
row --要修改的列(從1開始)
data --修改的內(nèi)容
返回值:成功返回0,失敗返回負(fù)數(shù)
*/
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);//文件指針轉(zhuǎn)文件描述符
if(fd==-1)
{
printf("轉(zhuǎn)換失敗\n");
return -1;
}
char *file_addr=mmap(NULL,flen,PROT_READ,MAP_SHARED,fd,0);//內(nèi)存映射
fclose(fp);
close(fd);
if(file_addr==NULL)
{
printf("映射失敗\n");
return -2;
}
/*統(tǒng)計(jì)行*/
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);//統(tǒng)計(jì)行
//"APPID","人臉庫","創(chuàng)建時(shí)間","用戶組ID","修改時(shí)間","用戶ID","修改時(shí)間","人臉url"
/*統(tǒng)計(jì)列*/
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文件數(shù)據(jù)保存到數(shù)組中*/
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
運(yùn)行結(jié)果:
-
C語言
+關(guān)注
關(guān)注
180文章
7595瀏覽量
135867 -
csv
+關(guān)注
關(guān)注
0文章
38瀏覽量
5797
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論