码迷,mamicode.com
首页 > 其他好文 > 详细

9.6 文件IO二进制读写

时间:2014-09-12 01:04:52      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   文件   

fseek(文件指针,偏移量,基准);  //偏移量的单位是字节
函数执行成功返回0,执行失败返回一个非0值
注意下面的特殊现象:
返回值是0      fseek(p,2,SEEK_END):  到终点了往后跳是成功的
返回值是-1      fseek(p,-2,SEEK_SET);在起始点往前面跳非法

windows的 b 模式 不影响 \r\n的偏移,只会按真实存在的字节偏移

ftell(p) 返回当前的文件指针位置
  1. #include <stdio.h>
  2. int main()
  3. {
  4. FILE *fp = fopen("a.txt","r+");
  5. if(!fp)
  6. {
  7. printf("打开文件失败\n");
  8. return -1;
  9. }
  10. int num = fseek(fp,2,SEEK_SET);
  11. printf("fseek = %d\n",num);
  12. printf("ftell = %ld\n",ftell(fp));
  13. char buf[1024];
  14. while(fgets(buf,sizeof(buf),fp) != NULL)
  15. {
  16. printf("%s\n",buf);
  17. }
  18. return 0;
  19. }
bubuko.com,布布扣

feof的一个特殊注意点:
  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. FILE *fp = fopen("a.txt","r");
  5. while(!feof(fp))
  6. {
  7. char tmp = 0;
  8. fread(&tmp,sizeof(tmp),1,fp);
  9. printf("%x\n",tmp);
  10. }
  11. fclose(fp);
  12. return 0;
  13. }
bubuko.com,布布扣
abc\n 后面多出一个0 是因为feof到了文件末尾并不返回1,只有越过了文件末尾才返回1。
即以上的fread多读了一次,多输出了一个0。
注意:在linux上统计行数也会多出一行。
解决方案:能不用feof就不用feof
  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. FILE *fp = fopen("a.txt","r");
  5. char tmp = 0;
  6. while(fread(&tmp,sizeof(tmp),1,fp) > 0)
  7. {
  8. printf("%x\n",tmp);
  9. }
  10. fclose(fp);
  11. return 0;
  12. }

fread  fwrite(重点):
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
fread()会返回实际读入的nmemb数目。
fwrite()会返回实际写入的nmemb数目。

fread 和 fwrite拷贝一个二进制文件
这样拷贝是不行的,b.pdf永远都会是1024的倍数
  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. FILE *fp1 = fopen("a.pdf","r");
  5. FILE *fp2 = fopen("b.pdf","w");
  6. char buf[1024];
  7. while(fread(buf,sizeof(buf),1,fp1) > 0)
  8. {
  9. fwrite(buf,sizeof(buf),1,fp2);
  10. }
  11. fclose(fp1);
  12. fclose(fp2);
  13. return 0;
  14. }
解决方案:
  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. FILE *fp1 = fopen("a.pdf","r");
  5. FILE *fp2 = fopen("b.pdf","w");
  6. char buf[1024];
  7. int len = 0;
  8. while((len = fread(buf,1,sizeof(buf),fp1)) > 0) //fread返回实际读入的第二个参数的数量
  9. {
  10. fwrite(buf,1,len,fp2); //我读了多少个字节,就写多少个字节
  11. }
  12. fclose(fp1);
  13. fclose(fp2);
  14. return 0;
  15. }

对二进制文件进行修改,检索,插入等操作
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. struct student
  5. {
  6. int ID;
  7. char name[20];
  8. };
  9. void init_student(char *filename)
  10. {
  11. struct student st[10] = {
  12. {1,"刘德华"},
  13. {2,"周杰伦"},
  14. {3,"成龙"},
  15. {4,"范冰冰"},
  16. {5,"柳岩"},
  17. {6,"杨幂"},
  18. {7,"林志玲"},
  19. {8,"韩寒"},
  20. {9,"普京"},
  21. {10,"赵本山"},
  22. };
  23. FILE *fp = fopen(filename,"w");
  24. if(!fp)
  25. {
  26. printf("打开文件失败\n");
  27. return;
  28. }
  29. fwrite(st,sizeof(struct student),10,fp);
  30. fclose(fp);
  31. }
  32. void show_student(char *filename) //循环输出所有学员信息
  33. {
  34. FILE * fp = fopen(filename,"r");
  35. if(!fp)
  36. {
  37. printf("打开文件失败\n");
  38. return;
  39. }
  40. struct student st;
  41. memset(&st,0,sizeof(st));
  42. while(fread(&st,sizeof(st),1,fp) > 0)
  43. {
  44. printf("%d:%s\n",st.ID,st.name);
  45. }
  46. fclose(fp);
  47. printf("***************\n");
  48. }
  49. void update_student(char * filename ,int n) //对二进制文件进行修改
  50. {
  51. FILE * fp = fopen(filename,"r+");
  52. if(!fp)
  53. {
  54. printf("打开文件失败\n");
  55. return;
  56. }
  57. fseek(fp,(n-1)*sizeof(struct student),SEEK_SET);
  58. struct student st = {100,"刘威"};
  59. fwrite(&st,sizeof(st),1,fp);
  60. fclose(fp);
  61. }
  62. void find_student(char *filename)           //对二进制文件进行检索
  63. {
  64. FILE * fp = fopen(filename,"r");
  65. if(!fp)
  66. {
  67. printf("打开文件失败\n");
  68. return;
  69. }
  70. int n = 0;
  71. printf("你输入你需要查找的学生编号:");
  72. scanf("%d",&n);
  73. fseek(fp,(n-1)*sizeof(struct student),SEEK_SET);
  74. struct student st;
  75. memset(&st,0,sizeof(st));
  76. fread(&st,sizeof(st),1,fp);
  77. printf("%d:%s\n",st.ID,st.name);
  78. fclose(fp);
  79. }
  80. void add_student(char *filename , int n) //对二进制文件进行插入
  81. {
  82. FILE * fp = fopen(filename,"r");
  83. if(!fp)
  84. {
  85. printf("打开文件失败\n");
  86. return;
  87. }
  88. struct student *p = calloc(100,sizeof(struct student));
  89. int index = 0,i = 0;
  90. while(fread(&p[index],sizeof(struct student),1,fp) > 0)
  91. index++;
  92. for(i = index ; i >= n ; i--)
  93. p[i+1] = p[i];
  94. p[n].ID = 88;
  95. strcpy(p[n].name,"习jin平");
  96. fclose(fp);
  97. fp = fopen(filename,"w");
  98. if(!fp)
  99. {
  100. printf("打开文件失败\n");
  101. return;
  102. }
  103. fwrite(p,sizeof(struct student),index+1,fp);
  104. fclose(fp);
  105. }
  106. int main(void)
  107. {
  108. init_student("a.bin");
  109. show_student("a.bin");
  110. //update_student("a.bin",4);
  111. add_student("a.bin",5);
  112. show_student("a.bin");
  113. //find_student("a.bin");
  114. return 0;
  115. }
bubuko.com,布布扣

把本班的同学读入到结构体数组里,加上序号,并且排序。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. struct student
  6. {
  7. int ID;
  8. char name[20];
  9. };
  10. void show_student()
  11. {
  12. FILE *fp = fopen("student.bin","r");
  13. while(!fp)
  14. {
  15. printf("打开文件失败\n");
  16. return;
  17. }
  18. struct student st;
  19. memset(&st,0,sizeof(st));
  20. while(fread(&st,sizeof(st),1,fp) > 0)
  21. {
  22. printf("%d:%s\n",st.ID,st.name);
  23. }
  24. }
  25. void sort_student()
  26. {
  27. FILE *fp = fopen("student.bin","r");
  28. while(!fp)
  29. {
  30. printf("打开文件失败\n");
  31. return;
  32. }
  33. struct student st[50];
  34. int index = 0;
  35. while(fread(&st[index],sizeof(struct student),1,fp) > 0)
  36. index++;
  37. int i,j,min;
  38. struct student tmp;
  39. for(i = 0 ; i < index ;i++)
  40. {
  41. min = i;
  42. for(j = i+1; j < index ; j++)
  43. if(st[min].ID > st[j].ID)
  44. min = j;
  45. if(min != i)
  46. {
  47. tmp = st[min];
  48. st[min] = st[i];
  49. st[i] = tmp;
  50. }
  51. }
  52. fclose(fp);
  53. fp = fopen("student.bin","w");
  54. for(i = 0 ; i < index; i++)
  55. fwrite(&st[i],sizeof(struct student),1,fp);
  56. fclose(fp);
  57. }
  58. int main(void)
  59. {
  60. srand((unsigned)time(NULL));
  61. FILE *fp1 = fopen("student.txt","r");
  62. FILE *fp2 = fopen("student.bin","w");
  63. while(!fp1 || !fp2)
  64. {
  65. printf("打开文件失败\n");
  66. return -1;
  67. }
  68. char buf[1024];
  69. struct student st;
  70. memset(&st,0,sizeof(st));
  71. int arr[42] = {0};
  72. int tmp;
  73. while(fgets(buf,sizeof(buf),fp1) != NULL)
  74. {
  75. if(buf[strlen(buf)-1] == ‘\n‘)
  76. buf[strlen(buf)-1] = ‘\0‘;
  77. tmp = rand()%42;
  78. while(arr[tmp] == 1)
  79. tmp = rand()%42;
  80. arr[tmp] = 1;
  81. st.ID = tmp;
  82. strcpy(st.name,buf);
  83. fwrite(&st,sizeof(st),1,fp2);
  84. }
  85. fclose(fp1);
  86. fclose(fp2);
  87. sort_student();
  88. show_student();
  89. return 0;
  90. }

bubuko.com,布布扣

bubuko.com,布布扣




9.6 文件IO二进制读写

标签:style   blog   http   color   io   os   ar   for   文件   

原文地址:http://www.cnblogs.com/l6241425/p/3967484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!