veselinsavov

3.25.15 PIK2

Mar 25th, 2015
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.82 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     FILE *txtFile = NULL, *binFile = NULL;
  6.     char *string[15];
  7.     int *firstIntArr, *secondIntArr, ch = 0, i, row = 0, srow = 0, cow = 0;
  8.     double *doubleArr;
  9.  
  10.     for (i = 0; i < 15; i++){
  11.         string[i] = (char *)malloc(15);
  12.     }
  13.  
  14.     firstIntArr = (int *)malloc(15 * sizeof(int));
  15.     secondIntArr = (int *)malloc(15 * sizeof(int));
  16.     doubleArr = (double *)malloc(15 * sizeof(double));
  17.  
  18.     txtFile = fopen("txtFile.txt", "r");
  19.     binFile = fopen("binFile", "wb");
  20.     if (binFile == NULL || txtFile == NULL) {
  21.         printf("File cannot be open!\n");
  22.         exit(1);
  23.     }
  24.  
  25.     printf("from txt file:\n");
  26.     while (ch != EOF)
  27.     {
  28.         while ((ch = fgetc(txtFile)) != ';' && ch != EOF) {
  29.             printf("%c", ch);
  30.             string[srow][cow] = (char)ch;
  31.             cow++;
  32.         }
  33.  
  34.         printf("\n");
  35.         while ((ch = fgetc(txtFile)) != ';' && ch != EOF) {
  36.             printf("%c", ch);
  37.             firstIntArr[row] = (int)ch;
  38.             row++;
  39.         }
  40.         printf("\n");
  41.  
  42.         row = 0;
  43.         while ((ch = fgetc(txtFile)) != ';' && ch != EOF) {
  44.             printf("%c", ch);
  45.             secondIntArr[row] = (int)ch;
  46.             row++;
  47.         }
  48.         printf("\n");
  49.  
  50.         row = 0;
  51.         while ((ch = fgetc(txtFile)) != ';' && ch != EOF) {
  52.             printf("%c", ch);
  53.             doubleArr[row] = (double)ch;
  54.             row++;
  55.         }
  56.  
  57.         srow++;
  58.         cow = 0;
  59.         printf("\n");
  60.     }
  61.     fclose(txtFile);
  62.  
  63.     for (i = 0; i<15; i++){
  64.         if (fwrite(&string[i], 1, sizeof(string), binFile) != sizeof(string)) {
  65.             printf("Err!");
  66.         }
  67.  
  68.         if (fwrite(&firstIntArr[i], sizeof(int), 1, binFile) != 1) {
  69.             printf("Err!");
  70.         }
  71.  
  72.         if (fwrite(&secondIntArr[i], sizeof(int), 1, binFile) != 1) {
  73.             printf("Err!");
  74.         }
  75.  
  76.         if (fwrite(&doubleArr[i], sizeof(double), 1, binFile) != 1) {
  77.             printf("Err!");
  78.         }
  79.     }
  80.     fclose(binFile);
  81.  
  82.     for (i = 0; i < 15; i++)
  83.     {
  84.         free(string[i]);
  85.     }
  86.  
  87.     free(firstIntArr);
  88.     free(secondIntArr);
  89.     free(doubleArr);
  90.  
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment