Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdlib.h>
- #include <string.h>
- #include "Header.h"
- char * MY_DATA::filename[] = {"my_file.bin"};
- FILE * MY_DATA::pf = NULL;
- bool OpenFile();
- void CloseFile();
- void crash(char *str);
- int _tmain(int argc, _TCHAR* argv[])
- {
- MY_DATA *tob = NULL;
- const size_t no_obj = 3;
- size_t i;
- char *sstr[] = {
- "aaaaaaaaaaaa",
- "bbb",
- "ccccccccccccccc"
- };
- //otwieramy plik
- if(!OpenFile())
- crash("open file error");
- //alokujemy pamiec dla tablicy obiektow MY_DATA
- tob = (MY_DATA *)malloc(no_obj*sizeof(MY_DATA));
- if(!tob)
- crash("memory allocation error");
- //Iniciujemy kazdy element tablicy
- for(i=0; i<no_obj; ++i)
- {
- if(!MY_DATA_Init(tob+i, sstr[i]))
- crash("MY_DATA init error");
- }
- //zapisujemy dane do pliku
- if(!MY_DATA_Write(tob))
- crash("write file error");
- //Wyczyszczamy tablice
- for(i=0; i<no_obj; ++i)
- {
- tob[i].str[0] = '\0';
- }
- //Wczytujemy elementy tablicy
- MY_DATA_PutFilePos(&tob[0]);
- if(!MY_DATA_Read(tob))
- crash("file read error");
- //porownujemy
- for(i=0; i<no_obj; ++i)
- {
- if(strcmp(tob[i].str, sstr[i]) != 0)
- printf("error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
- }
- //dealokujemy tablice
- if(tob)
- {
- for(i=0; i<no_obj; ++i)
- MY_DATA_Free(tob+i);
- free(tob);
- tob = NULL;
- }
- //zamykamy plik
- CloseFile();
- return 0;
- }
- bool OpenFile()
- {
- MY_DATA::pf = fopen(MY_DATA::filename[0], "w+b");
- if(!MY_DATA::pf)
- return 0;
- return 1;
- }
- void CloseFile()
- {
- if(MY_DATA::pf)
- fclose(MY_DATA::pf);
- MY_DATA::pf = NULL;
- }
- void crash(char *str)
- {
- printf("%s\n", str);
- system("pause");
- exit(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement