long filesize(char* file
Name);
char* file_get_contents(char* file
Name);
voID file_put_contents(char* fil
ename,char* data
);
#include <stdio.h>
#include <stdlib.h>
#include <
String.h>
long filesize(char* file
Name);
char* file_get_contents(char* file
Name);
voID file_put_contents(char* fil
ename,char* data
);int main()
{ printf("%s\n","----------------Begin---------------"
); char* fil
ename = "/tmp/tmp.txt";
file_put_contents(fil
ename,"
http://www.code.net"
); char* data = file_get_contents(file
Name);
printf("Fd:: %s\n",data
); printf("%s\n","----------------End-----------------"
); return 0;
}
long filesize(char* file
Name) { long length;
file* stream = fopen(fil
ename,"rb"
); if(!stream) return 0L;
fseek(stream,0L,SEEK_END
); length = ftell(stream
); fclose(stream
); return length;
}
char* file_get_contents(char* file
Name) { file* fp = fopen(fil
ename,"rb"
); if(!f
p) { printf("%s\n","The file can not be opened."
); exit(0
); }
long length = filesize(file
Name);
char* buffer = (char*) malloc(length
); char buf[1024];
memset(buffer,0x00,sizeof(buffer)
); fseek(fp,SEEK_SET
); while(fgets(buf,1024,f
p) != NulL)
strcat (buffer,buf
); fclose(f
p);
return buffer;
}
voID file_put_contents(char* fil
ename,char* data)
{ file* fp = fopen(fil
ename,"w+"
); if(!f
p) { printf("The file can not be opened.\n"
); exit
(1);
}
fputs(data,f
p);
fclose(f
p);
}