C Write / Read Data From Binary File

  c++, visual-c++

So I kinda read .txt file, unable to read second field year that is supposed to be 1995, but it is 0.

IBM HC-486 1995 61 12 228 Иванов

Making a binary file out of structs, attempting to print out all cointaining.. Getting all perfectly printed but, sadly second field with year is 0. I do not understand what is wrong, because it reads year perfectly.

ONLY scanf/printf/FILE/struct

Here’s a code…

Lab.h

#pragma once
    void input();
    void find();
    int getdays(int year, int month);
    void correction();
    void print();

Lab.cpp

#include "Lab.h"
#include <stdio.h>      //FILE
#include <iostream>
#include <conio.h>      //getch

#include <io.h>
struct Computer
{
    wchar_t mark[10];
    int year;
    int month;
    int day;
    unsigned char numroom;
    wchar_t lastname[20];
};
void input()
{
    FILE *inputFile, *outputFile;
    fopen_s(&outputFile, "output.dat", "wb");
    fopen_s(&inputFile, "input.txt", "r");
    fseek(inputFile, 0, SEEK_END);
    int size = ftell(inputFile);
    fseek(inputFile, 0, SEEK_SET);
    int item = 0;
    while (size > 0)
    {
        Computer c;
        fgetws(c.mark, 11, inputFile);
        fscanf_s(inputFile, "%d", &c.year);
        fscanf_s(inputFile, "%i", &c.month);
        fscanf_s(inputFile, "%i", &c.day);
        fscanf_s(inputFile, "%hhu", &c.numroom);
        fwscanf_s(inputFile, L"%s", c.lastname, _countof(c.lastname));

        c.mark[wcslen(c.mark) - 1] = '{$content}';

        wprintf(L"%d", c.year);

        fwrite(&c, sizeof(struct Computer), 1, outputFile);
        size -= sizeof(struct Computer);
        item += 1;
    }
    _fcloseall();
    return;
}
void find()
{
    FILE *outputFile;
    fopen_s(&outputFile, "output.dat", "rb+");
    Computer c;
    while (fread(&c, sizeof(struct Computer), 1, outputFile))
    {
        if (c.year == 1995 && wcscmp(L"IBM HC-486", c.mark))
        {
            wprintf_s(L"Запись найдена!");
            wprintf_s(L"mark = %s year = %i month = %i day = %i numroom = %i lastname = %s", 
                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 && 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(&outputFile, "output.dat", "rwb+");
    Computer c;
    long item = 0;
    while (fread(&c, sizeof(struct Computer), 1, outputFile))
    {
        while (c.month < 1 || c.month > 12)
        {
            wprintf_s(L"mark = %s year = %i month = %i day = %i numroom = %i lastname = %s",
                c.mark, c.year, c.month, c.day, c.numroom, c.lastname);
            wprintf_s(L"Некорректный номер месяца %inПожалуйста введите другой номер месяца:", c.month);
            scanf_s("%i", &c.month);
            fseek(outputFile, item * sizeof(struct Computer), 0);
            fwrite(&c, sizeof(struct Computer), 1, outputFile);
        }
        while (c.day < 1 || c.day > getdays(c.year, c.month))
        {
            wprintf_s(L"mark = %s year = %i month = %i day = %i numroom = %i lastname = %s",
                c.mark, c.year, c.month, c.day, c.numroom, c.lastname);
            wprintf_s(L"Некорректный номер дня %inПожалуйста введите другой номер дня:", c.day);
            scanf_s("%i", &c.day);
            fseek(outputFile, item * sizeof(struct Computer), 0);
            fwrite(&c, sizeof(struct Computer), 1, outputFile);
        }
        item += 1;
    }
    _getch();
    _fcloseall();
    return;
}
void print()
{
    FILE* outputFile;
    fopen_s(&outputFile, "output.dat", "rb+");
    Computer c;
    while (fread(&c, sizeof(struct Computer), 1, outputFile))
    {
        wprintf_s(L"mark = %s year = %d month = %i day = %i numroom = %i lastname = %s",
            c.mark, c.year, c.month, c.day, c.numroom, c.lastname);
    }
    _getch();
    _fcloseall();
    return;
}

Lab2.cpp

#include <windows.h>
#include "Lab.h"
int main()
{
    SetConsoleCP(65001);
    SetConsoleOutputCP(65001);

    input();
    print();

    //find();
    //correction();
    return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT