Advertisement
Iceek

ReadingFromBinary

Mar 27th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main()
  6. {
  7.     FILE *name1;
  8.     char **text=NULL;
  9.     int i,j,y;
  10.     int number = 0;
  11.     int count=0;
  12.     int temp_realloc;
  13.     int control_var = 5;
  14.  
  15.     text = (int*) malloc(sizeof(int*)*control_var);
  16.  
  17.     for(i=0; i < control_var ;i++)
  18.        {
  19.            text[i] = (char *) malloc(50);
  20.        }
  21.  
  22.     name1 = fopen("C:\\Users\\Iceekk\\Desktop\\traa\\versionTOMOV.bin", "rb");
  23.  
  24.         if(name1 == NULL)
  25.         {
  26.             printf("error with opening file for read..");
  27.             return -1;
  28.         }
  29.  
  30.         while(1)
  31.         {
  32.               if(fread(&number, sizeof(int) , 1 , name1) != 1)
  33.               {
  34.                 printf("\nSuccesfull Reading of .BIN FILE\n");
  35.                 break;
  36.               }
  37.  
  38.               if(fread(text[count],number,1,name1) != 1)
  39.               {
  40.                   printf("\nError while reading string! \n");
  41.                   exit(-1);
  42.                   break;
  43.               }
  44.  
  45.               for (j=0;  j<number ; j++ )
  46.               {
  47.                   printf("%c", text[count][j]);
  48.               }
  49.               printf("\n");
  50.             //  count++;
  51.  
  52.               if((count+1) >= control_var) // zavisi ot count !!!
  53.               {
  54.                   temp_realloc = count;
  55.                   control_var *= 2;
  56.                   text =(int *) realloc(text, sizeof(int*) *control_var);
  57.  
  58.                   for (y = temp_realloc ; y < control_var ; y++)
  59.                      {
  60.                       text[y] = (char *) malloc(30);
  61.                      }
  62.               }
  63.               count++;
  64.         }
  65.     fclose(name1);
  66.  
  67.     for (i=0;i<control_var;i++)
  68.     {
  69.         free(text[i]);
  70.     }
  71. free(text);
  72. text=NULL;
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement