A. 求c語言讀取寫入文本文件的函數實現
#include<stdio.h>
#include<string.h>//strlen()
#include<malloc.h>//malloc()
intFileCreate(char*filePath,char*fileContext)
{
FILE*fp;
fp=fopen(filePath,"w+");
if(fp)
{
fwrite(fileContext,strlen(fileContext),1,fp);
fclose(fp);
return0;
}
return1;
}
char*FileRead(char*filePath)
{
FILE*fp;
longintfsize;
char*fileContext;
fp=fopen(filePath,"r");
if(fp)
{
fseek(fp,0,SEEK_END);
fsize=ftell(fp);
rewind(fp);
fileContext=(char*)malloc(fsize+1);//注意釋放內存
fread(fileContext,fsize,1,fp);
*(fileContext+fsize)='