【c语言读取txt文件数据】C语言实现txt数据读入内存/CPU缓存实例详解

时间:2021-05-16  来源:C语言  阅读:

摘要

C实现将txt数据读入内存/CPU缓存的函数,不多说,实现如下。

1. 实现代码

   代码如下 #include "stdafx.h" #include #include    intfilelength(FILE*fp); char*readfile(char*path);       intmain(void){   char*string;      string=readfile("C:/Users/Joe WANG/Desktop/Data.txt");   printf("数据读入内存完毕! \n");   printf("内存中的数据如下:\n%s \n",string);   system("pause");        return0; }    char*readfile(char*path){   FILE*fp;     intlength;   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)="\0";        returnch; }    intfilelength(FILE*fp){   intnum;        fseek(fp,0,SEEK_END);   num=ftell(fp);   fseek(fp,0,SEEK_SET);        returnnum; }  

2. Data.txt中的源数据

3. 测试结果

【c语言读取txt文件数据】C语言实现txt数据读入内存/CPU缓存实例详解

http://m.bbyears.com/asp/117853.html

推荐访问:
相关阅读 猜你喜欢
本类排行 本类最新