c語言內存文件
發布時間: 2022-08-03 13:24:15
⑴ c語言 怎麼讀內存數據
分配內存空間然後讀取
#include<stdio.h>
#include<stdlib.h>
intfilelength(FILE*fp);
char*readfile(char*path);
intmain(void)
{
FILE*fp;
char*string;
string=readfile("c:/c.c");
printf("讀入完畢 按任意鍵釋放內存資源 ");
//printf("%s ",string);
system("pause");
return0;
}
char*readfile(char*path)
{
FILE*fp;
intlength;
char*ch;
if((fp=fopen(path,"r"))==NULL)
{
printf("openfile%serror. ",path);
exit(0);
}
length=filelength(fp);
ch=(char*)malloc(length);
fread(ch,length,1,fp);
*(ch+length-1)='