title: C Write / Read Data From Binary File
body: <p><strong>UPDATE</strong></p>
  <pre><code>IBM HC-486 1995 11 12 228 Иванов IBM HC-476 1990 1 42 218 Васильев
  </code></pre>
  <p>So i kinda try to read two records. First one fits out well. Second looks bad.
  I kinda fixed suggestions thanks a lot it helped to move forward. So for now i stuck on outputing two records.
  Result is -&gt;</p>
  <pre><code>mark = IBM HC-486 year = 1995 month = 11 day = 12 numroom = 228 lastname = Ивановmark =  IBM HC-47 year = 6 month = 1990
   day = 1 numroom = 42 lastname = 218mark =  Васи� year = 6 month = 1990 day = 1 numroom = 42 lastname = �ьев
  </code></pre>
  <p>Making a binary file out of structs, attempting to print out all cointaining..</p>
  <p><strong>ONLY scanf/printf/FILE/struct</strong></p>
  <p>Here's a code...</p>
  <p>Lab.h</p>
  <pre><code>#pragma once
      void input();
      void find();
      int getdays(int year, int month);
      void correction();
      void print();
  </code></pre>
  <p>Lab.cpp</p>
  <pre><code>#include &quot;Lab.h&quot;
  #include &lt;stdio.h&gt;      //FILE
  #include &lt;iostream&gt;
  #include &lt;conio.h&gt;      //getch
  #include &lt;windows.h&gt;
  #include &lt;io.h&gt;
  struct Computer
  {
      wchar_t mark[11];
      int year;
      int month;
      int day;
      unsigned char numroom;
      wchar_t lastname[20];
  };
  void input()
  {
      FILE *inputFile, *outputFile;
      fopen_s(&amp;outputFile, &quot;output.dat&quot;, &quot;wb&quot;);
      fopen_s(&amp;inputFile, &quot;input.txt&quot;, &quot;r&quot;);
      Computer c;
      while (fgetws(c.mark, 11, inputFile))
      {
          fscanf_s(inputFile, &quot;%d&quot;, &amp;c.year);
          fscanf_s(inputFile, &quot;%i&quot;, &amp;c.month);
          fscanf_s(inputFile, &quot;%i&quot;, &amp;c.day);
          fscanf_s(inputFile, &quot;%hhu&quot;, &amp;c.numroom);
          fwscanf_s(inputFile, L&quot;%s&quot;, c.lastname, _countof(c.lastname));
          fwrite(&amp;c, sizeof(struct Computer), 1, outputFile);
      }
      _fcloseall();
      return;
  }
  void find()
  {
      FILE *outputFile;
      fopen_s(&amp;outputFile, &quot;output.dat&quot;, &quot;rb+&quot;);
      Computer c;
      while (fread(&amp;c, sizeof(struct Computer), 1, outputFile))
      {
          if (c.year == 1995 &amp;&amp; wcscmp(L&quot;IBM HC-486&quot;, c.mark) == 0)
          {
              wprintf_s(L&quot;\nmark = %s year = %i month = %i day = %i numroom = %i lastname = %s&quot;, 
                  c.mark, c.year, c.month, c.day, c.numroom, c.lastname);
              _getch();
              _fcloseall();
              return;
          }
      }
      _getch();
      return;
  }
  
  int getdays(int year, int month)
  {
      int days = 0;
      if (month == 4 || month == 6 || month == 9 || month == 11)
          days = 30;
      else if (month == 2)
      {
          bool leapyear = (year % 4 == 0 &amp;&amp; year % 100 != 0) || (year % 400 == 0);
          if (leapyear == 0)
              days = 28;
          else
              days = 29;
      }
      else
          days = 31;
      return days;
  }
  
  void correction()
  {
      FILE* outputFile;
      fopen_s(&amp;outputFile, &quot;output.dat&quot;, &quot;rb+&quot;);
      fseek(outputFile, 0, 0);
      Computer c;
      long item = 0;
      while (fread(&amp;c, sizeof(struct Computer), 1, outputFile))
      {
  
          while (c.month &lt; 1 || c.month &gt; 12)
          {
              wprintf_s(L&quot;mark = %s year = %i month = %i day = %i numroom = %i lastname = %s&quot;,
                  c.mark, c.year, c.month, c.day, c.numroom, c.lastname);
              wprintf_s(L&quot;%s%i&quot;, L&quot;Некорректный номер месяца \nПожалуйста введите другой номер месяца:&quot;, c.month);
              scanf_s(&quot;%i&quot;, &amp;c.month);
              fseek(outputFile, item * sizeof(struct Computer), 0);
              fwrite(&amp;c, sizeof(struct Computer), 1, outputFile);
          }
          while (c.day &lt; 1 || c.day &gt; getdays(c.year, c.month))
          {
              wprintf_s(L&quot;mark = %s year = %i month = %i day = %i numroom = %i lastname = %s&quot;,
                  c.mark, c.year, c.month, c.day, c.numroom, c.lastname);
              wprintf_s(L&quot;%s%i&quot;, L&quot;Некорректный номер дня\nПожалуйста введите другой номер дня:&quot;, c.day);
              scanf_s(&quot;%i&quot;, &amp;c.day);
              fseek(outputFile, item * sizeof(struct Computer), 0);
              fwrite(&amp;c, sizeof(struct Computer), 1, outputFile);
          }
          item += 1;
      }
      _getch();
      _fcloseall();
      return;
  }
  void print()
  {
      FILE* outputFile;
      fopen_s(&amp;outputFile, &quot;output.dat&quot;, &quot;rb+&quot;);
      fseek(outputFile, 0, SEEK_SET);
      Computer c;
      while (fread(&amp;c, sizeof(struct Computer), 1, outputFile))
      {
          wprintf_s(L&quot;mark = %s year = %d month = %i day = %i numroom = %i lastname = %s&quot;,
              c.mark, c.year, c.month, c.day, c.numroom, c.lastname);
      }
      _getch();
      _fcloseall();
      return;
  }
  </code></pre>
  <p>Lab2.cpp</p>
  <pre><code>#include &lt;windows.h&gt;
  #include &quot;Lab.h&quot;
  int main()
  {
      SetConsoleCP(65001);
      SetConsoleOutputCP(65001);
  
      input();
      print();
  
      //find();
      //correction();
      return 0;
  }
  </code></pre>