Ошибка при удалении из файла
при удалении ошибка ,что я не так сделал ?
#include <iostream>
#include <fstream>
#include <cstring>
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <iomanip>
using namespace std;
struct Schoolboy
{
string f;
string i;
string o;
int Class{};
string PhoneNumber;
int GradeMaths{};
int GradePhysics{};
int GradeRussianLanguage{};
int GradeLiterature{};
};
ofstream file;
int ValidationGrade(int g)
{
cin >> g;
while (g < 0 || g > 5)
{
if (g <= 0)cout << "Error, the score cannot be less than 0. Please re-enter: ";
else cout << "Error, rating cannot be more than 5. Please re-enter:";
cin >> g;
}
return g;
}
void VvodFile(Schoolboy* Array, int size)
{
int g = 0;
fstream file("Schoolboy.txt", ios::out);
for (int i = 0; i < size; i++)
{
cout << "Enter F: ";
cin >> Array[i].f;
cout << "Enter I: ";
cin >> Array[i].i;
cout << "Enter O: ";
cin >> Array[i].o;
cout << "Enter Class: ";
cin >> Array[i].Class;
cout << "Enter Phone Number: ";
cin >> Array[i].PhoneNumber;
cout << "Enter Grade Maths: ";
Array[i].GradeMaths = ValidationGrade(g);
cout << "Enter Grade Physics: ";
Array[i].GradePhysics = ValidationGrade(g);
cout << "Enter Grade Russian Language: ";
Array[i].GradeRussianLanguage = ValidationGrade(g);
cout << "Enter Grade Literature: ";
Array[i].GradeLiterature = ValidationGrade(g);
file << Array[i].f;
file << "\n";
file << Array[i].i;
file << "\n";
file << Array[i].o;
file << "\n";
file << Array[i].Class;
file << "\n";
file << Array[i].PhoneNumber;
file << "\n";
file << Array[i].GradeMaths;
file << "\n";
file << Array[i].GradePhysics;
file << "\n";
file << Array[i].GradeRussianLanguage;
file << "\n";
file << Array[i].GradeLiterature;
file << "\n";
}
file.close();
}
void VivodFile()
{
Schoolboy p;
system("cls");
fstream file("Schoolboy.txt", ios::in);
do
{
file >> p.f;
file >> p.i;
file >> p.o;
file >> p.Class;
file >> p.PhoneNumber;
file >> p.GradeMaths;
file >> p.GradePhysics;
file >> p.GradeRussianLanguage;
file >> p.GradeLiterature;
if (file.eof())break;
cout << " " << p.f << " " << p.i << " " << p.o << " " << p.Class << " " << p.PhoneNumber << " " << p.GradeMaths << " " << p.GradePhysics << " " << p.GradeRussianLanguage << " " << p.GradeLiterature << "\n";
} while (!file.eof());
file.close();
}
void DeleteFile()
{
Schoolboy p;
system("cls");
fstream file("Schoolboy.txt", ios::in);
fstream filecopy("Schoolboycopy.txt", ios::app | ios::out);
do
{
file >> p.f;
file >> p.i;
file >> p.o;
file >> p.Class;
file >> p.PhoneNumber;
file >> p.GradeMaths;
file >> p.GradePhysics;
file >> p.GradeRussianLanguage;
file >> p.GradeLiterature;
if (file.eof())break;
if (p.GradeMaths > 2 && p.GradePhysics > 2 && p.GradeRussianLanguage > 2 && p.GradeLiterature > 2)
{
filecopy << p.f;
filecopy << "\n";
filecopy << p.i;
filecopy << "\n";
filecopy << p.o;
filecopy << "\n";
filecopy << p.Class;
filecopy << "\n";
filecopy << p.PhoneNumber;
filecopy << "\n";
filecopy << p.GradeMaths;
filecopy << "\n";
filecopy << p.GradePhysics;
filecopy << "\n";
filecopy << p.GradeRussianLanguage;
filecopy << "\n";
filecopy << p.GradeLiterature;
filecopy << "\n";
}
} while (!file.eof());
file.close();
filecopy.close();
rename("Schoolboy.txt", "Schoolboyold.txt");
remove("Schoolboy.txt");
rename("Schoolboycopy.txt", "Schoolboy.txt");
remove("Schoolboy.txt");
VivodFile();
}
int main()
{
int size = 0;
cout << "Enter size:";
cin >> size;
Schoolboy* Array = new Schoolboy[size];
VvodFile(Array, size);
VivodFile();
system("PAUSE");
cout << "Remove all items that have 2 of at least one item:";
DeleteFile();
system("PAUSE");
return 0;
}
Ответы (1 шт):
Автор решения: Павел Ериков
→ Ссылка
Ошибка в этих строчках:
rename("Schoolboy.txt", "Schoolboyold.txt");
remove("Schoolboy.txt");
rename("Schoolboycopy.txt", "Schoolboy.txt");
remove("Schoolboy.txt");
Сначала вы переименовываете файл Schoolboy, а потом пытаете удалить файл с таким же названием, а далее переименовываете Schoolboycopy и потом по факту удаляете этот файл.
Если я правильно вас понял, то вам нужно чтобы Schoolboycopy переименовался в Schoolboy, а после копия удалилась.
Тогда вот пример:
remove("Schoolboy.txt");
rename("Schoolboycopy.txt", "Schoolboy.txt");
Сначала мы убираем старую версию, и новую скопированную версию мы переименовываем на Schoolboy