标签:
hello world
这次课程设计要求,对文件进行操作
总结一下以后好用
主要是对下列函数的理解
1 //fgetc(fp) 2 /*FILE *fp = NULL; 3 char ch; 4 char filename[MAX] = { 0 }; 5 printf("please input file name\n"); 6 scanf("%s", filename); 7 fp = fopen(filename, "r"); 8 if (fp == NULL) 9 { 10 printf("open file error \n"); 11 return 0; 12 } 13 while (!feof(fp)) 14 { 15 ch = fgetc(fp); 16 printf("%c", ch); 17 } 18 fclose(fp); 19 system("pause"); 20 return 0;*/
1 //fputc(ch,fp) 2 /*FILE *fp = NULL; 3 char ch; 4 fp = fopen("stu.txt", "a"); 5 if (fp == NULL) 6 { 7 printf("open file error\n"); 8 return 0; 9 } 10 while (1) 11 { 12 ch = getchar(); 13 if (ch != ‘#‘) 14 { 15 fputc(ch, fp); 16 } 17 else 18 { 19 break; 20 } 21 22 } 23 fclose(fp); 24 system("pause"); 25 return 0;*/
1 //fgets,fputs 2 /*char srcfile[MAX] = { 0 }; 3 char desfile[MAX] = { 0 }; 4 char buff[MAX] = { 0 }; 5 FILE *srcfp = NULL; 6 FILE *desfp = NULL; 7 printf("please input src filename:"); 8 scanf("%s", srcfile); 9 printf("please input des filename:"); 10 scanf("%s", desfile); 11 srcfp = fopen(srcfile, "r"); 12 desfp = fopen(desfile, "w"); 13 if ((srcfp == NULL) || (desfp == NULL)) 14 { 15 printf("open file error \n"); 16 return 0; 17 } 18 while (!feof(srcfp)) 19 { 20 fgets(buff, 12, srcfp); 21 fputs(buff, desfp); 22 } 23 printf("copy succeed"); 24 fclose(desfp); 25 fclose(srcfp); 26 system("pause"); 27 return 0;*/
1 //fprintf() 2 /*int n = 0; 3 int i = 0; 4 FILE * fp = NULL; 5 fp = fopen("stu1.txt", "w"); 6 if (fp == NULL) 7 { 8 printf("open file error\n"); 9 return 0; 10 } 11 printf("please input number:"); 12 scanf("%d", &n); 13 for (i = 1; i < n; i++) 14 { 15 if (isPrime(i)) 16 { 17 fprintf(fp, "%d is prime\n", i); 18 } 19 } 20 printf("over!\n"); 21 fclose(fp); 22 system("pause"); 23 return 0;*/
1 //fscanf() 2 /*FILE * fp = NULL; 3 int n = 0; 4 char buff[5] = { 0 }; 5 fp = fopen("stu1.txt", "r"); 6 if (fp == NULL) 7 { 8 printf("open file error\n"); 9 return 0; 10 } 11 while (!feof(fp)) 12 { 13 fscanf(fp, "%d %s prime\n", &n,buff); 14 printf("%d %s\n", n,buff); 15 } 16 printf("\n"); 17 fclose(fp); 18 system("pause"); 19 return 0;*/
1 //fseek() 2 /*FILE *fp = NULL; 3 char buff[32] = { 0 }; 4 fp = fopen("stu1.txt", "r"); 5 if (fp == NULL) 6 { 7 printf("open file error\n"); 8 return 0; 9 } 10 fseek(fp, 10, SEEK_SET); 11 //fseek(fp, -10, SEEK_END); 12 for (int i = 0; i < 10; i++) 13 { 14 buff[i]= fgetc(fp); 15 } 16 printf("buf:%s", buff); 17 system("pause"); 18 return 0;*/ 19 //ftell() 20 /*FILE *fp = NULL; 21 char filename[32] = { 0 }; 22 long size = 0; 23 printf("please input filename:"); 24 scanf("%s", filename); 25 fp = fopen(filename, "r"); 26 if (fp == NULL) 27 { 28 printf("open file error\n"); 29 return 0; 30 } 31 fseek(fp, 0, SEEK_END); 32 size = ftell(fp); 33 fclose(fp); 34 printf("%s file size :%d", filename, size); 35 system("pause"); 36 return 0;*/ 37 //rewind() 38 /*char srcfile[MAX] = { 0 }; 39 char desfile[MAX] = { 0 }; 40 FILE *srcfp = NULL; 41 FILE *desfp = NULL; 42 printf("please input src filename:"); 43 scanf("%s", srcfile); 44 printf("please input des filename:"); 45 scanf("%s", desfile); 46 srcfp = fopen(srcfile, "r"); 47 desfp = fopen(desfile, "w"); 48 if ((srcfp == NULL) || (desfp == NULL)) 49 { 50 printf("open file error \n"); 51 system("puase"); 52 return 0; 53 } 54 while (!feof(srcfp)) 55 { 56 putchar(fgetc(srcfp)); 57 } 58 printf("\n"); 59 rewind(srcfp); 60 while (!feof(srcfp)) 61 { 62 fputc(fgetc(srcfp),desfp); 63 } 64 printf("copy succeed"); 65 fclose(desfp); 66 fclose(srcfp); 67 system("pause"); 68 return 0;*/
1 //二进制文件的读写fread() fwrite() 2 /*STUDENT studentArray[SIZE] = { { 0 } }; 3 int i = 0; 4 for (i = 0; i < SIZE; i++) 5 { 6 printf("please input student info:\n"); 7 scanf("%d %s %d %d %s", &(studentArray[i].id), studentArray[i].name, &(studentArray[i].age), 8 &(studentArray[i].score), studentArray[i].addr); 9 save(&studentArray[i]); 10 } 11 system("pause"); 12 return 0;*/ 13 /* STUDENT studentArray[SIZE] = { { 0 } }; 14 int i = 0; 15 load(studentArray); 16 for (i = 0; i < SIZE; i++) 17 { 18 printf("%d %s %d %d %s\n", studentArray[i].id, studentArray[i].name, studentArray[i].age, 19 studentArray[i].score, studentArray[i].addr); 20 } 21 system("pause"); 22 return 0;*/ 23 24 /*char filename[20] = { 0 }; 25 printf("please input delete file:"); 26 scanf("%s", filename); 27 if (0 == remove(filename)) 28 { 29 printf("ok"); 30 } 31 else 32 printf("failed"); 33 system("pause"); 34 return 0; 35 */
hello world
标签:
原文地址:http://www.cnblogs.com/windy13/p/5083323.html