C语言实现txt数据读入内存/CPU缓存实例详解
发布时间:2020-12-16 05:15:12 所属栏目:百科 来源:网络整理
导读:摘要 C实现将txt数据读入内存/CPU缓存的函数,不多说,实现如下。 1. 实现代码 #include "stdafx.h" #include stdio.h #include stdlib.h int filelength(FILE *fp); char *readfile(char *path); int main(void){ char *string; string=readfile("C:/Users/
摘要 C实现将txt数据读入内存/CPU缓存的函数,不多说,实现如下。 1. 实现代码 #include "stdafx.h" #include <stdio.h> #include <stdlib.h> int filelength(FILE *fp); char *readfile(char *path); int main(void){ char *string; string=readfile("C:/Users/Joe WANG/Desktop/Data.txt"); printf("数据读入内存完毕! n"); printf("内存中的数据如下:n%s n",string); system("pause"); return 0; } char *readfile(char *path){ FILE *fp; int length; char *ch; if((fp=fopen(path,"r"))==NULL){ printf("open file %s error.n",path); exit(0); } length=filelength(fp); ch=(char *)malloc(length); fread(ch,length,1,fp); *(ch+length)=' '; return ch; } int filelength(FILE *fp){ int num; fseek(fp,SEEK_END); num=ftell(fp); fseek(fp,SEEK_SET); return num; } 2. Data.txt中的源数据 3. 测试结果 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |