最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
C语言实现txt数据读入内存/CPU缓存实例详解
时间:2022-06-25 04:41:19 编辑:袖梨 来源:一聚教程网
摘要
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)=' ';
returnch;
}
intfilelength(FILE*fp){
intnum;
fseek(fp,0,SEEK_END);
num=ftell(fp);
fseek(fp,0,SEEK_SET);
returnnum;
}
|
2. Data.txt中的源数据
3. 测试结果
相关文章
- 王者荣耀10周年3千点券这么花-十周年3000点券使用建议 10-13
- 明日方舟真言值得抽吗-真言抽取建议 10-13
- 永劫无间手游南宫锦武器选哪个好-南宫锦武器选择推荐 10-13
- 王者荣耀月之回廊入口在哪-月之回廊入口位置介绍 10-13
- 羊蹄山之魂的时代背景是什么 羊蹄山之魂时代故事背景介绍 10-13
- 洛克王国世界吉格斯怎么打-吉格斯打法攻略 10-13