首页 > 代码库 > 8.5 文件IO fgets fputs fgetc fwrite fread

8.5 文件IO fgets fputs fgetc fwrite fread

fopen函数mode模式:

w+不是追加写 是多了一个读权限
文件指针+1没有意义


拷贝一个文件: fgets     fputs   (fgetc同理)
  1. int main()
  2. {
  3. FILE *fp, *fpcp;
  4. fp = fopen("yesteday_once_more.txt", "r");
  5. fpcp = fopen("a", "w");
  6. char buf[1024]; //一行给他1024个字节 尽量够用
  7. while (fgets(buf, sizeof(buf), fp) != NULL)
  8. {
  9. fputs(buf, fpcp);
  10. }
  11. fclose(fp);
  12. fclose(fpcp);
  13. return 0;
  14. }


获取用户键盘输入:

char buf[1024];
fgets( buf ,  sizeof(buf) ,  stdin );
fputs( buf , stdout);

把用户输入的记录到文件里,stdin就是键盘的输入
  1. #include <stdio.h>
  2. int main()
  3. {
  4. FILE *fp;
  5. fp = fopen("record", "a+");
  6. char buf[10];
  7. while (fgets(buf, sizeof(buf), stdin) != NULL)
  8. {
  9. fputs(buf,stdout);
  10. fputs(buf, fp); //linux下 ctrl+d才生效
  11. }
  12. fclose(fp);
  13. return 0;
  14. }


统计一个文件有多少行: fgets
  1. int line(char * s)
  2. {
  3. FILE *fp;
  4. fp = fopen(s, "r");
  5. char buf[1024];
  6. int n = 0;
  7. while (fgets(buf, sizeof(buf), fp) != NULL)
  8. n++;
  9. fclose(fp);
  10. return n;
  11. }
  12. int main()
  13. {
  14. int n = line("yesteday_once_more.txt");
  15. printf("%d\n", n);
  16. }

统计一个文件有多少个字节: fgetc
  1. int len(char * s)
  2. {
  3. FILE * fp;
  4. int n = 0;
  5. fp = fopen(s, "r");
  6. if (!fp)
  7. return -1;
  8. char ch;
  9. while ((ch = fgetc(fp)) != EOF)
  10. n++;
  11. fclose(fp);
  12. return n;
  13. }
  14. int main()
  15. {
  16. int n = len("yes.txt");
  17. printf("%d\n", n);
  18. return 0;
  19. }

fread的说明:  
如果此函数用来拷贝,步长大了,容易丢失数据
长处在于读写结构体。
写入的是二进制文件    类似序列化的理念,更强大


fread读到末尾返回0  fgets 读到末尾返回NULL  fgetc读到末尾返回 EOF  [-1]


从一个文本文件读到结构体数组里,然后再fwrite到二进制文件,再fread到结构体数组
并增加了排序,用了函数指针
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. typedef struct STU
  6. {
  7. int id;
  8. char name[20];
  9. char sex;
  10. int score;
  11. } *pS;
  12. int count_stu(FILE * fp)
  13. {
  14. char buf[1024];
  15. int n = 0;
  16. while (fgets(buf, sizeof(buf), fp) != NULL)
  17. n++;
  18. rewind(fp);
  19. return n / 5;
  20. }
  21. int file_stu(FILE * fp, pS a, int n)
  22. {
  23. int i = 0;
  24. char buf[1024];
  25. for (i = 0; i < n; i++)
  26. {
  27. fgets(buf, sizeof(buf), fp);
  28. a[i].id = atoi(buf);
  29. fgets(a[i].name, sizeof(a[i].name), fp);
  30. strtok(a[i].name, "\n");
  31. fgets(buf, sizeof(buf), fp);
  32. a[i].sex = buf[0];
  33. fgets(buf, sizeof(buf), fp);
  34. a[i].score = atoi(buf);
  35. fgets(buf, sizeof(buf), fp);
  36. }
  37. rewind(fp);
  38. return i;
  39. }
  40. void show_stu(pS a, int n)
  41. {
  42. int i = 0;
  43. for (i = 0; i < n; i++)
  44. {
  45. printf("%d %s %c %d\n", a[i].id, a[i].name, a[i].sex, a[i].score);
  46. }
  47. printf("***************\n");
  48. return;
  49. }
  50. //此函数第一种写法
  51. int stu_bin(FILE * fp, pS a, int n)
  52. {
  53. fwrite(a, sizeof(*a), n, fp);
  54. rewind(fp);
  55. return 0;
  56. }
  57. //此函数第二种写法
  58. int stu_bin(FILE *fp, pS a, int n)
  59. {
  60. int i;
  61. for (i = 0; i < n; i++)
  62. {
  63. fwrite(&a[i], sizeof(a[i]), 1, fp);
  64. }
  65. rewind(fp);
  66. return i;
  67. }
  68. int bin_stu(FILE * fp, pS a, int n)
  69. {
  70. fread(a, sizeof(*a), n, fp);
  71. rewind(fp);
  72. return 0;
  73. }
  74. int sort_by_id(pS a, pS b)
  75. {
  76. return a->id - b->id;
  77. }
  78. int sort_by_name(pS a, pS b)
  79. {
  80. return strcmp(a->name, b->name);
  81. }
  82. int sort_by_sex(pS a, pS b)
  83. {
  84. return a->sex - b->sex;
  85. }
  86. int sort_by_score(pS a, pS b)
  87. {
  88. return a->score - b->score;
  89. }
  90. void sort_stu(pS a, int n, int(*fun)(pS a, pS b))
  91. {
  92. int i, j, min;
  93. struct STU temp;
  94. for (i = 0; i < n; i++)
  95. {
  96. min = i;
  97. for (j = i + 1; j < n; j++)
  98. {
  99. if (fun(&a[min], &a[j])>0)
  100. {
  101. min = j;
  102. }
  103. }
  104. temp = a[min];
  105. a[min] = a[i];
  106. a[i] = temp;
  107. }
  108. return;
  109. }
  110. int main(void)
  111. {
  112. FILE * fp = fopen("stu.txt", "r");
  113. FILE * fp_bin = fopen("stu.bin", "w+"); //注意这里是w+,如果是w的话就不能读了,注意坑
  114. if (!fp || !fp_bin)
  115. return -1;
  116. int count = count_stu(fp);
  117. struct STU * a = (pS)malloc(count * sizeof(*a));
  118. file_stu(fp, a, count);
  119. //show_stu(a, count);
  120. stu_bin(fp_bin, a, count);
  121. memset(a, 0, (count * sizeof(*a)));
  122. bin_stu(fp_bin, a, count);
  123. fclose(fp);
  124. fclose(fp_bin);
  125. show_stu(a, count);
  126. sort_stu(a, count, sort_by_id);
  127. show_stu(a, count);
  128. sort_stu(a, count, sort_by_name);
  129. show_stu(a, count);
  130. sort_stu(a, count, sort_by_sex);
  131. show_stu(a, count);
  132. sort_stu(a, count, sort_by_score);
  133. show_stu(a, count);
  134. free(a);
  135. }


去除字符串的\n :           

一个是
strtok(a[i].name, "\n");

另外一个是,
len = strlen(buf);
buf[len - 1] = ‘\0‘;



来自为知笔记(Wiz)


附件列表

     

    8.5 文件IO fgets fputs fgetc fwrite fread