c语言实现通用文件读取(兼容大小和格式)
1.新建一个1.txt文件,里面写入内容
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a6409843c321d1881bdec23056c6028b.jpeg#pic_center)
2.根据读取的内容执行写入到另外的文件2.txt
3.代码如下:
#include <stdio.h>
#include<malloc.h>
int getfulesize(char *file);
int readfiledata(char *filename,char *filedata);
int writetofile(char *filedata, int size, char *outfilename);
int readfiledata(char *filename, char *filedata)
{
FILE *fpin;
int getfulesizes = getfulesize(filename);
fpin = fopen(filename,"rb");
fread(filedata,1,getfulesizes,fpin);
fclose(fpin);
}
int writetofile(char *filedata, int size,char *outfilename)
{
FILE *fout;
fout = fopen(outfilename,"w+");
fwrite(filedata,1,size,fout);
fclose(fout);
}
int getfulesize(char *file)
{
FILE *fil;
fil = fopen(file,"rb");
fseek(fil,0L,SEEK_END);
int getfulesize = ftell(fil);
fseek(fil,0,0);
return getfulesize;
}
int main()
{
char * sourcefile = "1.txt",*outFile = "2.txt";;
int filedestination = getfulesize( sourcefile);
char *file_data=malloc(filedestination );
readfiledata( sourcefile, file_data);
writetofile(file_data,filedestination ,outFile);
return 0;
}