fgetc 统计行号
23 FILE* fp=fopen("./1.c","r");24 if(fp==0)25 {26 perror("错误原因");27 return -1;28 }29 int count=0;30 while(1)31 {32 char retval=fgetc(fp);33 if(retval==EOF)34 {35 break;36 }37 else if(retval=='\n')38 {39 count++;40 }41 }42 printf("count=%d\n",count);43 fclose(fp);44 45 return 0;46 }
fgetc/fputc 文件拷贝
23 FILE* rfp=fopen("./1.c","r");24 FILE* wfp=fopen("./11.c","w");25 if(rfp==0||wfp==0)26 {27 perror("错误原因");28 return -1;29 }30 while(1)31 {32 char ch=fgetc(rfp);33 if(ch==EOF)34 {35 break;36 }37 fputc(ch,wfp);38 }39 fclose(rfp);40 fclose(wfp);41 return 0; 42 }
fscanf/fprintf 文件拷贝
21 int main(int argc, const char *argv[])22 {23 FILE* rfp=fopen("11.c","r");24 FILE* wfp=fopen("111.c","w");25 if(rfp==0||wfp==0)26 {27 perror("错误原因");28 return -1;29 }30 while(1)31 {32 char ch=0;33 int retval=fscanf(rfp,"%c",&ch);34 if(retval==EOF)35 {36 break;37 }38 fprintf(wfp,"%c",ch); 39 }
fgets读的过程
23 FILE* fp=fopen("./1.c","r");24 if(fp==0)25 {26 perror("错误原因");27 return -1;28 }29 char str[10]="";30 while(1)31 {32 char* p=fgets(str,10,fp);33 if(p==NULL)34 {35 break;36 }37 printf("%s\n",str);38 } 39 // printf("%s\n",str);40 fclose(fp);41 return 0;42 }
fgets统计行号
24 FILE* fp=fopen("./1.c","r");25 if(fp==0)26 {27 perror("错误原因");28 return -1;29 }30 int count=0;31 char str[2]="";32 while(1)33 {34 char* p=fgets(str,2,fp);35 if(p==NULL)36 {37 break;38 }39 else if(str[0]=='\n')40 {41 count++;42 }43 }44 printf("count=%d\n",count);45 return 0;46 }
fread/fwrite/fseek 红色旗
23 FILE* fp=fopen("./SYK6XNF[HINYIE}OP)_6I96.bmp","r");24 int size=0;25 int width=0;26 int height=0;27 fseek(fp,2,SEEK_SET);28 fread(&size,4,1,fp);29 printf("图片总大小为:%d\n",size);30 31 fseek(fp,18,SEEK_SET);32 fread(&width,4,1,fp);33 fread(&height,4,1,fp);34 printf("width=%d,height=%d\n",width,height);35 36 unsigned char bgr[3]={0,0,255};37 fp=fopen("./SYK6XNF[HINYIE}OP)_6I96.bmp","r+");38 fseek(fp,54,SEEK_SET);39 for(int i=0;i<width;i++)40 {41 for(int j=0;j<height;j++)42 { 43 fwrite(bgr,3,1,fp);44 }45 }46 fclose(fp);