摘要
C實(shí)現(xiàn)將txt數(shù)據(jù)讀入內(nèi)存/CPU緩存的函數(shù),不多說(shuō),實(shí)現(xiàn)如下。
1. 實(shí)現(xiàn)代碼
#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("數(shù)據(jù)讀入內(nèi)存完畢! /n"); printf("內(nèi)存中的數(shù)據(jù)如下:/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; } 2. Data.txt中的源數(shù)據(jù)

3. 測(cè)試結(jié)果

新聞熱點(diǎn)
疑難解答
圖片精選