首页 > 其他分享 >C语言读写表格文件

C语言读写表格文件

时间:2023-03-11 14:31:42浏览次数:43  
标签:11 01 file 表格 ++ 读写 C语言 2022 printf

1.csv文件简介

  逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样被解读的数据。CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗号或制表符。通常,所有记录都有完全相同的字段序列。通常都是纯文本文件。建议使用WORDPAD或是记事本来开启,再则先另存新档后用EXCEL开启,也是方法之一。

   CSV文件格式的通用标准并不存在,但是在RFC 4180中有基础性的描述。使用的字符编码同样没有被指定,但是bitASCII是最基本的通用编码。

  CSV是一种通用的、相对简单的文件格式,被用户、商业和科学广泛应用。最广泛的应用是在程序之间转移表格数据,而这些程序本身是在不兼容的格式上进行操作的(往往是私有的和/或无规范的格式)。因为大量程序都支持某种CSV变体,至少是作为一种可选择的输入/输出格式。

 “CSV”并不是一种单一的、定义明确的格式(尽管RFC 4180有一个被通常使用的定义)。因此在实践中,术语“CSV”泛指具有以下特征的任何文件:

  1. 纯文本,使用某个字符集,比如ASCII、Unicode、EBCDIC或GB2312;
  2. 由记录组成(典型的是每行一条记录);
  3. 每条记录被分隔符分隔为字段(典型分隔符有逗号、分号或制表符;有时分隔符可以包括可选的空格);
  4. 每条记录都有同样的字段序列。

    在这些常规的约束条件下,存在着许多CSV变体,故CSV文件并不完全互通。然而,这些变异非常小,并且有许多应用程序允许用户预览文件(这是可行的,因为它是纯文本),然后指定分隔符、转义规则等。如果一个特定CSV文件的变异过大,超出了特定接收程序的支持范围,那么可行的做法往往是人工检查并编辑文件,或通过简单的程序来修复问题。因此在实践中,CSV文件还是非常方便的。

 2.csv文件规则

  1. 开头是不留空,以行为单位。
  2. 可含或不含列名,含列名则居文件第一行。
  3. 一行数据不跨行,无空行。
  4. 以半角逗号(即,)作分隔符,列为空也要表达其存在。
  5. 列内容如存在半角引号(即’),替换成半角双引号(“)转义,即用半角引号(即")将该字段值包含起来。
  6. 文件读写时引号,逗号操作规则互逆。
  7. 内码格式不限,可为 ASCII、Unicode 或者其他。
  8. 不支持数字
  9. 不支持特殊字符

3. csv文件示例

"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"

  如上所示即为csv文件,第一行为列名,每列通过逗号(,)隔开,内容保存在双引号(" ")中,上面文件是通过Notepad++打开看到的效果,使用EXCEL打开看到格式如下:

C语言读写表格文件_C/C++

4.读取csv文件示例

#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;
}

运行结果:


C语言读写表格文件_CSV_02

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;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);/*取消映射*/
char buff2[file_line][cnt][500];/*保存修改后的文件数据*/
for(i=0;i<file_line;i++)
{
//printf("\n-------------------------------\n");
for(j=0;j<cnt-1;j++)
{
sprintf(buff2[i][j],"\"%s\",",buff[i][j]);
//printf("%s",buff2[i][j]);
}
if(i==file_line-1 && j==cnt-1)
{
sprintf(buff2[i][j],"\"%s\"",buff[i][j]);
}
else sprintf(buff2[i][j],"\"%s\"\n",buff[i][j]);
//printf("%s",buff2[i][j]);
//printf("\n---------------------------------\n");
}
fp = fopen(filename,"w+b");
if(fp==NULL)
{
printf("文件打开失败\n");
return -3;
}
if(line>file_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<file_line;i++)
{
for(j=0;j<cnt;j++)
{
size=fwrite(buff2[i][j],1,strlen(buff2[i][j]),fp);
if(size!=strlen(buff2[i][j]))
{
printf("size=%d,%ld\n",size,strlen(buff2[i][j]));
printf("写入失败\n");
}
}
}
return 0;
}
int main()
{
printf("\n---------------修改前-------------------\n");
readfile_csv("1.csv");
printf("\n----------------------------------------\n");
modifyfile_csv("1.csv",7,8,"http:www.baidu.com");/*将第7行第8列数据改为http:www.baidu.com*/
printf("\n---------------修改后-------------------\n");
readfile_csv("1.csv");
printf("\n----------------------------------------\n");
return 0;
}

运行结果:

C语言读写表格文件_CSV_03

标签:11,01,file,表格,++,读写,C语言,2022,printf
From: https://blog.51cto.com/u_15688123/6114594

相关文章