visoft

Pregatire 19 Nov, Read and Write a binary structure

Nov 19th, 2020
479
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX_LINE_LEN 10000
  6. #define MAX_NO_LINES 100
  7.  
  8. /*
  9.  * On success, the function returns str.
  10. If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof).
  11.  If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged).
  12. If a read error occurs, the error indicator (ferror) is set and a null pointer is also returned (but the contents pointed by str may have changed).
  13.  *
  14.  */
  15.  
  16. /*
  17.  * void* malloc (size_t size);
  18.  *
  19.  *
  20.  */
  21.  
  22.  
  23. typedef struct{
  24.     char first_chars[100];
  25.     int line_length;
  26.     int no_characters;
  27. }LineInfo;
  28.  
  29. int main() {
  30.     FILE * f = fopen("../in.txt", "r");
  31.     if (f == NULL)
  32.         return 100;
  33.  
  34.     char **tab = (char **)malloc(sizeof(char*)*MAX_NO_LINES);
  35.     char* linie = (char *)malloc(sizeof(char)* MAX_LINE_LEN);
  36.     char* ok=linie;
  37.     int n = 0;
  38.     while (ok == linie){
  39.         ok = fgets(linie, MAX_LINE_LEN, f);
  40.         if(ok != linie)
  41.             break;
  42.         tab[n] = (char*)malloc(sizeof(char) * (strlen(linie) + 1));
  43.         strcpy(tab[n], linie);
  44.         n++;
  45.     }
  46.     free(linie);
  47.     fclose(f);
  48.     //**************************************************  AICI AM CONTINUTUL IN MEMORIE ************//
  49.  
  50.     FILE * fout = fopen("../out.txt", "wb");
  51.     if (fout == NULL)
  52.         return 110;
  53.  
  54.     for(int i=0;i<n;i++){
  55.         //Sa numaram spatiile
  56.         int nr_spatii = 0;
  57.         for(int j = 0; j < strlen(tab[i]);j++){
  58.             int is_whitespace = tab[i][j] == ' ' || tab[i][j] == '\n' || tab[i][j] == '\r';
  59.             if(!is_whitespace)
  60.                 nr_spatii++;
  61.         }
  62.         LineInfo li;
  63.         li.line_length = strlen(tab[i]);
  64.         li.no_characters = nr_spatii;
  65.         memset(li.first_chars, 0, 100);
  66.         strncpy(li.first_chars, tab[i], 99);
  67.         fwrite(&li, sizeof(LineInfo), 1, fout);
  68. //        fprintf(fout,"Number of non-spaces: %-6d : %s", nr_spatii, tab[i]);  <-- Here I am outputing variable length text.
  69.     }
  70.     fclose(fout);
  71.  
  72.     //!!!!!!!!!!!!!!!!!
  73.     // I have n lines. So in the file I will have n structures.
  74.  
  75.     FILE * fin = fopen("../out.txt", "rb");
  76.     if (fin == NULL)
  77.         return 110;
  78.  
  79.     LineInfo *linfo = (LineInfo*)malloc(sizeof(LineInfo) * n);
  80.     fread(linfo, sizeof(LineInfo), n, fin);
  81.     for(int i=0; i<n;i++){
  82.         printf("Read from file: %d : %d %s\n", linfo[i].no_characters, linfo[i].line_length, linfo[i].first_chars);
  83.     }
  84.  
  85.  
  86.  
  87.     for(int i=0;i<n;i++){
  88.         free(tab[i]);
  89.     }
  90.     free(tab);
  91.  
  92.     return 0;
  93. }
  94.  
  95.  
RAW Paste Data