Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
3.1 kB
2
Indexable
Never
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct Student
{
    int StudentId;
    int GroupNumber;
    string Faculty;
    string StudentName;
};

int CountSize(ifstream&, Student*, string);
void ReadFile(ifstream&, Student*, string, int&);
void FillFile(ofstream&, Student*, int);
int SizeOfFile(ifstream&);
void ReverseStrings(ofstream&, Student*, string, int);

int main()
{
    setlocale(LC_ALL, "rus");

    ofstream fileOutput;
    ifstream fileInput;
    ofstream& outObject = fileOutput;
    ifstream& inObject = fileInput;

	int arraySize;

	string fileName;
	
	
	do
	{
		cout << "Введите название файла, который вы хотите открыть" << endl;
		cin >> fileName;
		fileInput.open(fileName);
	} while (!(fileInput.is_open()));
	fileInput.close();

	Student* student = new Student;
	arraySize = CountSize(inObject, student, fileName);
	int& size = arraySize;
	delete student;

	Student* students = new Student[arraySize];

	ReadFile(inObject, students, fileName, size);
	FillFile(outObject, students, arraySize);

	cout << "размер файла: " << SizeOfFile(fileInput) << " байт";

	ReverseStrings(outObject, students, fileName, arraySize);

	delete[] students;
}

int CountSize(ifstream& inObject, Student* student, string fileName)
{
	int sizeOfFile = 0;

	inObject.open(fileName);
	while (!inObject.eof())
	{
		inObject >> student->StudentId;
		inObject >> student->GroupNumber;
		inObject >> student->Faculty;
		inObject >> student->StudentName;
		sizeOfFile++;
	}
	
	inObject.close();

	return sizeOfFile;
}

void ReadFile(ifstream& inObject, Student* students, string fileName, int& size)
{
	int i = 0;
	inObject.open(fileName);


	while (!inObject.eof())
	{
		inObject >> students[i].StudentId;
		inObject >> students[i].GroupNumber;
		inObject >> students[i].Faculty;
		inObject >> students[i].StudentName;
		i++;
	}
	if (students[i-1].Faculty == "")
	{
		size--;
	}

	inObject.close();
}

void FillFile(ofstream& outObject, Student* students, int size)
{
	outObject.open("NewFile.txt");
	size--;

	while (size >= 0)
	{
		outObject << students[size].StudentId << " ";
		outObject << students[size].GroupNumber << " ";
		outObject << students[size].Faculty << " ";
		outObject << students[size].StudentName << '\n';
		size--;
	}

	outObject.close();
}

int SizeOfFile(ifstream& inObject)
{
	inObject.open("NewFile.txt", ios_base::binary);
	inObject.seekg(0, ios_base::end);
	int size = inObject.tellg();
	inObject.close();
	return size;
}

void ReverseStrings(ofstream& outObject, Student* students, string fileName, int size)
{
	outObject.open("NewFile.txt");

	size--;
	while (size >= 0)
	{
		outObject << students[size].StudentId << " ";
		outObject << students[size].GroupNumber << " ";
		reverse(students[size].Faculty.begin(), students[size].Faculty.end());
		outObject << students[size].Faculty << " ";
		reverse(students[size].StudentName.begin(), students[size].StudentName.end());
		outObject << students[size].StudentName << '\n';
		size--;
	}

	outObject.close();
}