我以前写过一篇类似的文章:https://blog.csdn.net/morixinguan/article/details/83309576关于文件操作,尤其是从头到尾的阅读,如果您像上述文章那样操作,效率显然太低。

如果数据很多,则很难处理。

因此,我想到了使用更好的数据结构来解决此问题,您只是想从头到尾显示它吗?然后,您可以使用链接列表来解决此问题。

& nbsp;& nbsp;& nbsp; typedef结构链接{int size& nbsp ;;& nbsp;无效* ptr;& nbsp;结构链接结构链接* pre& nbsp ;; }链接;链表的数据结构,ptr是要存储的数据,pre是前任指针,next是后继指针。

通过这两个指针,可以轻松实现链表的遍历。

& nbsp;  以下定义了要实现的功能:typedef& nbsp; void(* print_t)(void * Data)& nbsp;& nbsp; void print(void * Data); //将链接列表节点打印为空& nbsp; print_links(LINKS * Header,print_t& nbsp; func); //创建链接列表标题LINKS& nbsp; * Create_Links_Header(int size); // Header insert void& nbsp; top_append(LINKS * Header,void * Data,int size); //获取文件中的总行数int GetTotalLineCount(FILE * file); & nbsp;& nbsp;& nbsp;总体实施:#include< stdio.h& gt; #include& lt; stdlib.h& gt; #include& lt; string.h& gt; #define& nbsp; NR(x)& nbsp; (sizeof(x)/ sizeof(x [0] +0))& nbsp; typedef结构链接{int size& nbsp ;;& nbsp;无效* ptr;& nbsp;结构链接结构链接* pre& nbsp ;; }链接; & nbsp;类型定义& nbsp; void(* print_t)(void *数据)& nbsp ;;& nbsp; void print(void * Data);无效print_links(LINKS *标题,print_t& nbsp; func); LINKS& nbsp; * Create_Links_Header(int size); void& nbsp; top_append(LINKS * Header,void * Data,int size); int GetTotalLineCount(FILE *文件); & nbsp; & nbsp; int main(void){int line = 0; int file_all_line = 0; char line_buffer [50] = {0};链接* Header = NULL; // 1。

初始化链接列表标头=& nbsp; Create_Links_Header(0); if(NULL ==标头){fprintf(stderr," malloc& nbsp;标头失败 ”); return -1;& nbsp;} // 2。

插入数据FILE * FP = NULL; fp = fopen(“ 1.csv”,“ r”); if(NULL == fp){printf("打开CSV文件失败! ”); return -1;} //移动到文件的开头fseek(fp,0,SEEK_SET); //获取文件中的总行数file_all_line = GetTotalLineCount(fp); for(line = 0; line& lt; file_all_line; line ++){if(fgets(line_buffer,50,fp)){printf(“ line_buffer:%s”,line_buffer); top_append(Header,line_buffer,100);}} print_links(Header,print); fclose(fp); free(标题);返回0;}& nbsp; void print(void * Data){printf(“%s”,Data); //可以在这里完成数据处理... //可以在这里进行数据处理..} //打印链接列表节点void print_links(LINKS * Header,print_t func){LINKS * tmp = Header-& gt;下一个& nbsp; while(tmp!= Header){func(tmp-& ptr); tmp = tmp-> next; & nbsp; & nbsp; & nbsp; & nbsp; free(tmp-& gt; pre);}} //获取文件中的总行数int GetTotalLineCount(FILE * file){& nbsp;& nbsp; int line_num = 0; char strLine [50]; fseek(文件,0,SEEK_SET); while(fgets(strLine,50,文件))line_num ++; fseek(文件,0,SEEK_SET); return line_num;} //创建标题LINKS& nbsp; * Create_Links_Header(int size){LINKS * New = NULL& nbsp ;;& nbsp;新= malloc(sizeof(LINKS)); if(NULL == New){fprintf(stderr," malloc LINKS标头失败 ”);返回NULL;& nbsp;}& nbsp;新大小=大小; nbsp; New-ptr = NULL;  New-> next = New;新的& gt; =新的& nbsp; & nbsp; return New;} //插入链接列表的开头void& nbsp; top_append(LINKS * Header,void * Data,int size){LINKS * New = NULL;& nbsp;新= malloc(sizeof(LINKS)); if(NULL == New){fprintf(stderr," malloc链接失败 ”); return;} New-& gt; ptr = NULL;新大小=大小; nbsp; & nbsp; New-ptr = malloc(size); if(NULL == New-& gt; ptr){fprintf(stderr,“ malloc链接数据失败 ”); return;& nbsp;} memcpy(New-ptr,Data,size); New-next = Header-next;  New-& gt;& nbsp; =标头;& nbsp; New-> next-& gt; pre = New; & nbsp; New-> pre-& gt; next = New;& nbsp;}& nbsp;运行结果:excel文件的数据如下图所示:运行程序以获取:从这里看,整个程序基于堆栈的思想,