实例1:读写字符文件,每次读取一个字符。
#include <stdio.h>
int main()
{
FILE *fpin ;
FILE *fpout;
char c;
fpout=fopen("c:\\dest.txt","wt");
if((fpin=fopen("c:\\test.txt","rt"))!=NULL)
{
c =fgetc(fpin);
while(c!=EOF)
{
fputc(c,fpout);
c=fgetc(fpin);
}
}
else
{
printf("file not exist!");
exit(1);
}
fclose(fpin);
fclose(fpout);
return 0;
}
实例2:读取字符文件,每次读入一个缓存里面。
#include <stdio.h>
#define MAXLEN 1024
int main()
{
FILE *fin;
FILE *fout=fopen("c:\\dest.txt","wt");
char buf[MAXLEN];
if((fin=fopen("c:\\test.txt","rt"))!=NULL)
{
char* c =fgets(buf,MAXLEN,fin);
while(c!=0)
{
fputs(buf,fout);
c=fgets(buf,MAXLEN,fin);
}
}
else
{
printf("file not exist!");
exit(1);
}
fclose(fin);
fclose(fout);
return 0;
}
实例3:读写字节文件,每次读入一个缓存里面。
#include <stdio.h>
#define MAXLEN 1024
int main()
{
FILE *fpin ;
FILE *fpout;
unsigned char buf[MAXLEN];
int c;
fpout=fopen("c:\\dest.jpg","wb");
if((fpin=fopen("c:\\test.jpg","rb"))!=NULL)
{
c = fread(buf,sizeof(unsigned char),MAXLEN,fpin);
while(c!=0)
{
fwrite(buf,sizeof(unsigned char),c,fpout);
c=fread(buf,sizeof(unsigned char),MAXLEN,fpin);
}
}
else
{
printf("file not exist!");
exit(1);
}
fclose(fpin);
fclose(fpout);
return 0;
}