From ac5bdbaebbdd7b30987c38fc3f7041cc0079c907 Mon Sep 17 00:00:00 2001 From: Misha Date: Thu, 6 Nov 2025 19:27:10 +0200 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20=C2=AB?= =?UTF-8?q?/=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FractionBinaryFile.cpp | 127 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 FractionBinaryFile.cpp diff --git a/FractionBinaryFile.cpp b/FractionBinaryFile.cpp new file mode 100644 index 0000000..70b5807 --- /dev/null +++ b/FractionBinaryFile.cpp @@ -0,0 +1,127 @@ +#include +#include +using namespace std; + +int ncd(int a, int b) { + return (b == 0 ? a : ncd(b, a % b)); +} + +struct Drib { + int ch; + int zn; + + void init() { + cout << "\nInput ch: "; + cin >> ch; + cout << "Input zn: "; + cin >> zn; + skor(); + } + void skor() { + int d = ncd(ch, zn); + if (d != 0) { + ch /= d; + zn /= d; + } + } + void print() { + cout << ch << "/" << zn << endl; + } + +}; +int main() +{ + Drib a; + Drib* pDrip = new Drib; + fstream finOut; + + finOut.open("info.dat", ios::binary | ios::out);//ios::out|ios::trunc); + if (!finOut.is_open()) { + cerr << "\n Error opening file(out)!!\n"; + system("pause"); + return 1; + } + int vUser; + do { + + a.init(); + pDrip->init(); + finOut.write((char*)pDrip, sizeof(Drib)); + //finOut.write(reinterpret_cast(&a), sizeof(a)); + finOut.write((char*)(&a), sizeof(Drib)); + cout << "\n Create a fraction?(1/Yes 0/No) "; + cin >> vUser; + } while (vUser); + finOut.close(); + + /*ifstream fIn; + finOut.open("info.dat", ios::binary );*/ + + finOut.open("info.dat", ios::binary | ios::in); + if (!finOut.is_open()) { + cerr << "\n Beda(in)!!"; + system("pause"); + return 1; + } + cout << "\nRead fractions in file:\n"; + finOut.read((char*)(&a), sizeof(a));//1 Drip + while (finOut.good()) {//!finOut.eof() + a.print(); + finOut.read(reinterpret_cast(&a), sizeof(a)); + } + finOut.close(); + system("pause"); + Drib* p; + finOut.open("info.dat", ios::binary | ios::in); + if (!finOut.is_open()) { + cerr << "\n Error opening file(in)!!"; + system("pause"); + return 1; + } + finOut.seekg(0, ios::end);//ios::beg ios::cur + int endpos = (int)finOut.tellg(); + int n = endpos / sizeof(Drib); + cout << "\n There are " << n << " fractions in the file\n"; + if (n == 0) { + return 0; + } + finOut.seekg(0);//finOut.seekg(0, ios::beg); + p = new Drib[n]; + int i = 0; + for (i = 0; i < n; ++i) { + finOut.read(reinterpret_cast(p + i), sizeof(Drib)); + } + + cout << "Counted " << i << " fractions\n"; + for (i = 0; i < n; ++i) { + p[i].print(); + + } + + //finOut.seekg(sizeof(drob)*(n-1)); + finOut.seekg(-(streamoff)sizeof(Drib), ios::end); + finOut.read(reinterpret_cast(&a), sizeof(Drib)); + cout << "\n The last fraction:\n"; + a.print(); + delete[] p; + finOut.close(); + a.init(); + finOut.open("info.dat", ios::binary | ios::app); + finOut.write((char*)(&a), sizeof(a)); + //finOut.seekp(0, ios::end); + endpos = (int)finOut.tellp(); + n = endpos / sizeof(Drib); + cout << "\n There are " << n << " fractions in the file\n"; + finOut.close(); + finOut.open("info.dat", ios::binary | ios::in); + if (!finOut.is_open()) { + cerr << "\n Error: bad open file(in)!!"; + system("pause"); + return 1; + } + finOut.seekg(-(streamoff)sizeof(Drib), ios::end); + finOut.read(reinterpret_cast(&a), sizeof(Drib)); + cout << "\n The last fraction:\n"; + a.print(); +} +