Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "Header.h"
  5.  
  6. char * MY_DATA::filename[] = {"my_file.bin"};
  7. FILE * MY_DATA::pf = NULL;
  8.  
  9. bool OpenFile();
  10. void CloseFile();
  11. void crash(char *str);
  12.  
  13. int _tmain(int argc, _TCHAR* argv[])
  14. {
  15. MY_DATA *tob = NULL;
  16. const size_t no_obj = 3;
  17. size_t i;
  18. char *sstr[] = {
  19. "aaaaaaaaaaaa",
  20. "bbb",
  21. "ccccccccccccccc"
  22. };
  23.  
  24. //otwieramy plik
  25. if(!OpenFile())
  26. crash("open file error");
  27.  
  28. //alokujemy pamiec dla tablicy obiektow MY_DATA
  29. tob = (MY_DATA *)malloc(no_obj*sizeof(MY_DATA));
  30. if(!tob)
  31. crash("memory allocation error");
  32.  
  33. //Iniciujemy kazdy element tablicy
  34. for(i=0; i<no_obj; ++i)
  35. {
  36. if(!MY_DATA_Init(tob+i, sstr[i]))
  37. crash("MY_DATA init error");
  38. }
  39.  
  40. //zapisujemy dane do pliku
  41. if(!MY_DATA_Write(tob))
  42. crash("write file error");
  43.  
  44. //Wyczyszczamy tablice
  45. for(i=0; i<no_obj; ++i)
  46. {
  47. tob[i].str[0] = '\0';
  48. }
  49.  
  50. //Wczytujemy elementy tablicy
  51. MY_DATA_PutFilePos(&tob[0]);
  52. if(!MY_DATA_Read(tob))
  53. crash("file read error");
  54.  
  55. //porownujemy
  56. for(i=0; i<no_obj; ++i)
  57. {
  58. if(strcmp(tob[i].str, sstr[i]) != 0)
  59. printf("error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  60. }
  61.  
  62. //dealokujemy tablice
  63. if(tob)
  64. {
  65. for(i=0; i<no_obj; ++i)
  66. MY_DATA_Free(tob+i);
  67. free(tob);
  68. tob = NULL;
  69. }
  70.  
  71. //zamykamy plik
  72. CloseFile();
  73.  
  74. return 0;
  75. }
  76.  
  77. bool OpenFile()
  78. {
  79. MY_DATA::pf = fopen(MY_DATA::filename[0], "w+b");
  80. if(!MY_DATA::pf)
  81. return 0;
  82. return 1;
  83. }
  84.  
  85. void CloseFile()
  86. {
  87. if(MY_DATA::pf)
  88. fclose(MY_DATA::pf);
  89. MY_DATA::pf = NULL;
  90. }
  91.  
  92. void crash(char *str)
  93. {
  94. printf("%s\n", str);
  95. system("pause");
  96. exit(1);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement