Advertisement
weeez

Beadandó - file olvasás

Oct 16th, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h> // for open()
  4. #include <errno.h> // for perror()
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <string.h> //for strcpy()
  8. #include <unistd.h> //for file write()
  9.  
  10. struct Kivansag {
  11.   double date;
  12.   char name[100];
  13.   char city[100];
  14.   char address[100];
  15.   char gift[100];
  16. };
  17.  
  18. int main(){ //18
  19.  
  20.   //const char *array[] = {"almafa\n", "kortefa\n", NULL};
  21.   int list_size = 0;
  22.   FILE *list = fopen("kivansagok", "wb");
  23.  
  24.     struct Kivansag kivansag = {1000,"TestName", "TestCity", "TestAddress", "TestGift"};
  25.     struct Kivansag kivansag2 = {2000,"TestName", "TestCity", "TestAddress", "TestGift"};
  26.     ++list_size;
  27.     ++list_size;
  28.     struct Kivansag asd[2]= {kivansag, kivansag2};
  29.     fwrite(&list_size, sizeof(list_size),1,list);
  30.     fwrite(&asd, sizeof(asd),1,list);
  31.  
  32.     printf("%lu \n", sizeof(list));
  33.  
  34.   fclose(list);
  35.  
  36.   struct Kivansag test;
  37.   struct Kivansag dsa[22];
  38.   list_size = 0;
  39.   list = fopen("kivansagok", "rb");
  40.     fread(&list_size, sizeof(list_size), 1, list);
  41.     fread(&dsa, sizeof(dsa), 1, list);  
  42.   fclose(list);
  43.     printf("size: %d \n", list_size);
  44.     printf("teszt: %f %s %s %s %s \n ",
  45.       dsa[0].date,
  46.       dsa[0].name,
  47.       dsa[0].city,
  48.       dsa[0].address,
  49.       dsa[0].gift);
  50.    
  51.  
  52.   return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement