Advertisement
Guest User

Untitled

a guest
May 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4.  
  5. void random(float arr[],int n);
  6. void print(float arr[],int n);
  7. int text_size();
  8. int binary_size();
  9. void file_size_print();
  10.  
  11. int main()
  12. {
  13.     float tab[100];
  14.     random(tab,100);
  15.     print(tab,100);
  16.     file_size_print();
  17.    
  18.     return 0;
  19. }
  20.  
  21. void random(float arr[],int n)
  22. {
  23.     srand(time(NULL));
  24.     int i=0;
  25.     for (i; i<n; i++) arr[i]=rand()%100;
  26. }
  27.  
  28. void print(float arr[],int n)
  29. {
  30.     FILE * fp = fopen("wynik_binarny.bin", "wb");
  31.     FILE * fp1 = fopen("wynik_tekstowy.txt", "w");
  32.     int i=0;
  33.     fwrite(arr,sizeof(float),100,fp);
  34.     for (i; i<n; i++)
  35.     {
  36.         printf("[%.1f]",arr[i]);
  37.         fprintf (fp1, "%.1f\n",arr[i]);
  38.     }
  39.     fclose(fp);
  40.     fclose(fp1);
  41.    
  42.     FILE * _fp = fopen("wynik_binarny.bin", "rb");
  43.     FILE * _fp1 = fopen("wynik_tekstowy.txt", "r");
  44.     fread(arr,sizeof(float),100,_fp);
  45.     for(i=0; i<100; i++)printf("[%.1f]",arr[i]);
  46.     for(i=0; i<100; i++)
  47.     {
  48.         fscanf(_fp1,"%.1f\n",arr[i]);
  49.         printf("[%.1f]",arr[i]);
  50.     }
  51.     fclose(_fp);
  52.     fclose(_fp1);
  53. }
  54. int text_size()
  55. {
  56.     FILE *fp = fopen("wynik_tekstowy.txt", "r");
  57.     fseek(fp, 0, SEEK_END);
  58.     int lengthOfFile = ftell(fp);
  59.     fclose(fp);
  60.     free(fp);
  61.     return lengthOfFile;
  62. }
  63. int binary_size()
  64. {
  65.     FILE *fp = fopen("wynik_binarny.bin", "rb");
  66.     fseek(fp, 0, SEEK_END);
  67.     int lengthOfFile = ftell(fp);
  68.     fclose(fp);
  69.     free(fp);
  70.     return lengthOfFile;
  71. }
  72. void file_size_print()
  73. {
  74.     int size_txt=text_size();
  75.     int size_bin=binary_size();
  76.     printf("\n\nRozmiar 1 %d\nRozmiar 2 %d\n",size_txt,size_bin);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement