栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言实现txt数据读入内存/CPU缓存实例详解

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言实现txt数据读入内存/CPU缓存实例详解

摘要

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

1. 实现代码

#include "stdafx.h" 
#include  
#include  
 
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,0,SEEK_END); 
  num=ftell(fp); 
  fseek(fp,0,SEEK_SET); 
   
  return num; 
} 

2. Data.txt中的源数据

3. 测试结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/63936.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号