Ребята, кто разбирается в работе с stdio.h в C++?
Скину код для наглядности. Дело в том, что он абсолютно идентичен у меня, и у товарища, однако если у моего знакомого он запускается, то мне, после ввода основных параметров и работы функции добавления данных структуры в бинарный файл, выдаётся такая ошибка.
Сразу говорю, мне советовали проверить версию Visual Studio и компилятор. Они у нас с ним абсолютно идентичны: Visual Studio 2022 и версия компилятора c++ 17. Ошибка возникает в момент использования функции, где записаны стандартные fopen, fwrite и fclose.
Сам код:
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <stdio.h>
#include "windows.h"
using namespace std;
struct TV_channels {
//char label[40];
int short unsigned hours_of_begin;
int short unsigned minutes_of_begin;
unsigned short int channel;
};
void AddChannel(TV_channels channel, const char* path)
{
FILE* f = fopen(path, "ab+");
fwrite(&channel, sizeof(TV_channels), 1, f);
fclose(f);
cout << "Succesfull adding new channel!" << endl;
}
void OutChannelList(const char* path)
{
int unsigned short counter = 1;
FILE* f = fopen(path, "ab+");
TV_channels temp;
while (fread(&temp, sizeof(temp), 1, f)!=0)
{
cout << counter <<". " << //temp.label
" | " << temp.hours_of_begin << ":" << temp.minutes_of_begin << " | " << "Channel: " << temp.channel << " | " << endl;
counter++;
}
fclose(f);
}
void ClearChannelList(const char* path)
{
FILE* f = fopen(path, "wb");
fclose(f);
cout << "Succesful clearing!" << endl;
}
void OneChannel(unsigned short int index,const char* path)
{
FILE* f = fopen(path, "rb");
TV_channels temp;
rewind(f);
for (int i = 1; i < index; i++)
{
fseek(f, sizeof(TV_channels), SEEK_CUR);
}
fread(&temp, sizeof(temp), 1, f);
cout << //temp.label
" | " << temp.hours_of_begin << ":" << temp.minutes_of_begin << " | " << "Channel: " << temp.channel << " | " << endl;
fclose(f);
}
void ChangeByIndex(unsigned short int index, const char* path, int short unsigned hours_of_begin, int short unsigned minutes_of_begin, unsigned short int channel)
{
FILE* f = fopen(path, "rb+");
TV_channels temp = { hours_of_begin , minutes_of_begin, channel };
rewind(f);
for (int i = 1; i < index; i++)
{
fseek(f, sizeof(TV_channels), SEEK_CUR);
}
fwrite(&temp, sizeof(temp), 1, f);
fclose(f);
}
int main()
{
const char* path = "C:\\Prorgamms\\Utilites\\Code_Programms\\c++\\2lab_bin\\file.bin";
unsigned short int choose;
unsigned short int index;
int short unsigned hours_of_begin;
int short unsigned minutes_of_begin;
unsigned short int channel;
//char label[40];
while (true)
{
cout << "1. Add new channel" << endl;
cout << "2. Output channel list" << endl;
cout << "3. Clear channel list" << endl;
cout << "4. Output one channel" << endl;
cout << "5. Change by index" << endl;
cout << ">";
cin >> choose;
system("cls");
switch (choose)
{
case 1:
{
//cout << "Input channel label: ";
//cin >> label;
cout << "Input hours of begin: ";
cin >> hours_of_begin;
cout << "Input minutes of begin: ";
cin >> minutes_of_begin;
cout << "Input channel number: ";
cin >> channel;
TV_channels temp = { //label ,
hours_of_begin ,minutes_of_begin, channel };
AddChannel(temp, path);
break;
}
case 2:
{
OutChannelList(path);
break;
}
case 3:
{
ClearChannelList(path);
break;
}
case 4:
{
cout << "Input channel index: ";
cin >> index;
OneChannel(index, path);
break;
}
case 5:
{
cout << "Input channel index, you want to chage: ";
cin >> index;
cout << "You want to change";
OneChannel(index, path);
cout << "New hours of begin: ";
cin >> hours_of_begin;
cout << "New minutes of begin: ";
cin >> minutes_of_begin;
cout << "New channel number: ";
cin >> channel;
ChangeByIndex(index, path, hours_of_begin, minutes_of_begin, channel);
break;
}
case 6:
{
}
default:
cout << "Unknown command, input correct command!" << endl;
Sleep(2000);
break;
}
Sleep(2000);
system("cls");
}
}