Posted By

oronm on 07/11/09


Tagged


Versions (?)

fseek


 / Published in: C
 

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. void main(int argc,char **argv)
  5. {
  6. int len;
  7. FILE *fp;
  8. int val;
  9. int size;
  10.  
  11. fp = fopen(argv[1], "wb");
  12. val = 52;
  13. fwrite(&val, sizeof(int), 1, fp);
  14. val = 122;
  15. fwrite(&val, sizeof(int), 1, fp);
  16. val = 21;
  17. fwrite(&val, sizeof(int), 1, fp);
  18. fclose(fp);
  19.  
  20. val = 0;
  21.  
  22.  
  23. val = sizeof(int);
  24. printf("%d ", val);
  25. fp = fopen(argv[1], "rb");
  26. fread(&val, sizeof(int), 1, fp);
  27. printf("%d ", val);
  28. //fseek(fp,4,SEEK_SET);
  29. if(feof(fp))
  30. printf("game over");
  31. else
  32. {
  33. fread(&val, sizeof(int), 1, fp);
  34. printf("%d ", val);
  35. }
  36.  
  37. //fseek(fp,8,SEEK_SET);
  38. if(feof(fp))
  39. printf("game over");
  40. else
  41. {
  42. fread(&val, sizeof(int), 1, fp);
  43. printf("%d ", val);
  44. }
  45.  
  46. fread(&val, sizeof(int), 1, fp);
  47. printf("XX %d ", val);
  48.  
  49. if(feof(fp))
  50. printf("game over");
  51.  
  52.  
  53. fseek(fp,0,SEEK_END);
  54.  
  55. size = ftell(fp);
  56. if(feof(fp))
  57. printf("game over");
  58.  
  59. printf(" %d ", size);
  60. fread(&val, sizeof(int), 1, fp);
  61. printf("%d ", val);
  62. fread(&val, sizeof(int), 1, fp);
  63. printf("%d ", val);
  64. fseek(fp, -4, SEEK_CUR);
  65. fread(&val, sizeof(int), 1, fp);
  66. printf("%d ", val);
  67. fseek(fp, -8, SEEK_CUR);
  68. fread(&val, sizeof(int), 1, fp);
  69. printf("%d ", val);
  70.  
  71.  
  72. /*while(!feof(fp))
  73. {
  74.  
  75. }*/
  76.  
  77. fclose(fp);
  78. }

Report this snippet  

You need to login to post a comment.