示例代码如下:
#include<stdio.h>
#include<string.h>
void main()
{
FILE *fp;
char buf[]="hellonihao";
//
char buf1[20]="";
fp=fopen("1.txt","w");
fwrite(buf,1,strlen(buf)+1,fp);
fclose(fp);
fp=fopen("1.txt","r");
fseek(fp, 0, SEEK_SET);
fread(buf1,1,strlen("wangkechuang")+1,fp);
printf("%s\n",buf1);
fclose(fp);
/
char buf2[20]=" ";
fp=fopen("2.txt","w");
fprintf(fp,"%s","wangkechuang");
fclose(fp);
fp=fopen("2.txt","r");
// fseek(fp, 0, SEEK_SET);
fscanf(fp,"%s",buf2);
printf("%s\n",buf2);
char c;
fp=fopen("3.txt","w");
fputc('w',fp);
fclose(fp);
fp=fopen("3.txt","r");
c=fgetc(fp);
printf("%c\n",c);
fclose(fp);
char name[20]="";
fp=fopen("4.txt","w");
fputs("wangkechuang",fp);
fclose(fp);
fp=fopen("4.txt","r");
fgets(name,sizeof("wangkechuang"),fp);
printf("%s\n",name);
}