c语言读写文件操作
1 #include <stdio.h> 2 #include <math.h> 3 #include <stdlib.h> 4 void writefile() 5 { 6 FILE *fp; 7 fp = fopen("e://ss.txt","w+"); 8 if(fp == NULL) 9 { 10 exit(0); 11 } 12 int i=2, a=100, flag=0,count = 0; 13 for (a=2;a<=1000;a++) 14 { 15 flag=0; 16 i= 2; 17 while (i<=sqrt(a)) 18 { 19 if (a%i==0) 20 { 21 flag=1; 22 break; 23 } 24 i++; 25 } 26 if (flag==0) 27 { 28 count++; 29 // printf("%5d",a); 30 fprintf(fp,"%5d",a); 31 if(count % 10 == 0) 32 { 33 // printf("\n"); 34 fprintf(fp,"\n"); 35 } 36 } 37 } 38 fclose(fp); 39 } 40 void readfile() 41 { 42 char str[1024]; 43 FILE *fp; 44 fp = fopen("e://ss.txt","r"); 45 if(fp == NULL) 46 { 47 exit(0); 48 } 49 while(!feof(fp)) 50 { 51 fgets(str,1024,fp); 52 printf("%s", str); 53 } 54 printf("\n"); 55 fclose(fp); 56 } 57 int main() 58 { 59 writefile(); 60 readfile(); 61 return 0; 62 }