Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.77 KB | None | 0 0
  1. ///Header
  2. #ifndef HEADER_H_INCLUDED
  3. #define HEADER_H_INCLUDED
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <limits.h>
  8. #include <string.h>
  9.  
  10.  
  11. struct info {
  12.     int st;
  13.     float nd;
  14.     char *rd;
  15. };
  16.  
  17. void find_info(char* file, struct info *data);
  18. void save(char *name, struct info *data);
  19.  
  20. #endif
  21.  
  22.  
  23.  
  24. ///Main
  25. #include "header.h"
  26.  
  27. int main(/*int argc, char** argv*/)
  28. {
  29.     /*if (argc < 2) {
  30.         printf("Not enough arguments!\n");
  31.         exit(0);
  32.     }*/
  33.  
  34.     struct info *transfer = {0};
  35.     transfer = (struct info*) malloc(sizeof(struct info));
  36.  
  37.     find_info("data.txt", transfer);
  38.  
  39.     save("generated.dat", transfer);
  40.  
  41.     free(transfer);
  42.  
  43.     return 0;
  44. }
  45.  
  46. void save(char *name, struct info *data)
  47. {
  48.     FILE* fp = fopen(name, "wb");
  49.  
  50.     (void)fwrite(&data, sizeof(data), 1, fp);
  51.  
  52.     //free(temp_string);
  53.     //free(data.rd);
  54.     fclose(fp);
  55. }
  56.  
  57.  
  58.  
  59. ///Nd
  60. #include "header.h"
  61.  
  62. void find_info(char* file, struct info *data)
  63. {
  64.     FILE* fp = fopen(file, "r");
  65.  
  66.     if (fp == NULL) {
  67.         (void)printf("File not found!\n");
  68.         exit(1);
  69.     }
  70.  
  71.     int temp_int = 0;
  72.     float temp_float = INT_MIN;
  73.     char *temp_string = NULL;
  74.  
  75.     while (fscanf(fp, "%d", &temp_int) != EOF) {
  76.         temp_string = (char*)malloc(sizeof(char) * (temp_int + 1));
  77.  
  78.         if (temp_string == NULL) {
  79.             (void)printf("Memory not allocated!\n");
  80.             exit(2);
  81.         }
  82.  
  83.         (void)fscanf(fp, " | %s | %f\n", temp_string, &temp_float);
  84.  
  85.         if (temp_float > data->st) {
  86.             data->st = temp_int;
  87.             data->nd = temp_float;
  88.             data->rd = temp_string;
  89.             (void)printf("%d | %s | %f\n", data->st, data->nd, data->rd);
  90.         }
  91.     }
  92.  
  93.     free(temp_string);
  94.     fclose(fp);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement