源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-03-31 01:31 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C语言实现txt数据读入内存/CPU缓存实例详解
[b]摘要[/b] C实现将txt数据读入内存/CPU缓存的函数,不多说,实现如下。 [b]1. 实现代码 [/b]
#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)='\0'; 
   
  return ch; 
} 
 
int filelength(FILE *fp){ 
  int num; 
   
  fseek(fp,0,SEEK_END); 
  num=ftell(fp); 
  fseek(fp,0,SEEK_SET); 
   
  return num; 
} 

[b]2. Data.txt中的源数据[/b] [img]http://files.jb51.net/file_images/article/201701/201713110340210.png?2017031141[/img] [b]3. 测试结果[/b] [img]http://files.jb51.net/file_images/article/201701/201713110412224.png?20170311425[/img]
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部