Advertisement
DaniDori

BinaryFileManagment.cpp

Nov 2nd, 2023
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.76 KB | None | 0 0
  1. #include "BinaryFileManagement.h"
  2. #include <fstream>
  3. #include <iostream>
  4. #include <sstream>
  5.  
  6. void BinaryFile::createFromText(const string& textFileName, const string& binaryFileName) {
  7.     ifstream textFile(textFileName);
  8.     ofstream binaryFile(binaryFileName, ios::binary);
  9.  
  10.     if (!textFile.is_open() || !binaryFile.is_open()) {
  11.         cerr << "Failed to open files!" << endl;
  12.         return;
  13.     }
  14.  
  15.     string line;
  16.     while (getline(textFile, line)) {
  17.         // Находим позицию первого пробела
  18.         size_t pos = line.find(' ');
  19.         if (pos != string::npos) {
  20.             // Разделяем строку на английское и русское слово
  21.             string englishWord = line.substr(0, pos);
  22.             string russianWord = line.substr(pos + 1);
  23.  
  24.             DictionaryEntry entry(englishWord, russianWord);
  25.             binaryFile.write(reinterpret_cast<char*>(&entry), sizeof(entry));
  26.         }
  27.     }
  28.  
  29.     textFile.close();
  30.     binaryFile.close();
  31. }
  32.  
  33.  
  34. void BinaryFile::addRecord(const string& binaryFileName, DictionaryEntry& entry) {
  35.     ofstream binaryFile(binaryFileName, ios::binary | ios::app);
  36.     if (!binaryFile.is_open()) {
  37.         cerr << "Failed to open binary file!" << endl;
  38.         return;
  39.     }
  40.  
  41.     binaryFile.write(reinterpret_cast<char*>(&entry), sizeof(entry));
  42.     binaryFile.close();
  43. }
  44.  
  45. void BinaryFile::removeRecord(const string& binaryFileName, const string& key) {
  46.     ifstream binaryFile(binaryFileName, ios::binary);
  47.     ofstream tempFile("temp.bin", ios::binary);
  48.  
  49.     if (!binaryFile.is_open() || !tempFile.is_open()) {
  50.         cerr << "Failed to open files!" << endl;
  51.         return;
  52.     }
  53.  
  54.     DictionaryEntry entry;
  55.     while (binaryFile.read(reinterpret_cast<char*>(&entry), sizeof(entry))) {
  56.         if (strncmp(entry.englishWord.data(), key.c_str(), entry.englishWord.size()) != 0) {
  57.             tempFile.write(reinterpret_cast<char*>(&entry), sizeof(entry));
  58.         }
  59.     }
  60.  
  61.     binaryFile.close();
  62.     tempFile.close();
  63.  
  64.     // Переименовываем tempFile в binaryFileName
  65.     remove(binaryFileName.c_str());
  66.     rename("temp.bin", binaryFileName.c_str());
  67. }
  68.  
  69. void BinaryFile::readRecord(const string& binaryFileName, int recordNumber) {
  70.     ifstream binaryFile(binaryFileName, ios::binary);
  71.     if (!binaryFile.is_open()) {
  72.         cerr << "Failed to open binary file!" << endl;
  73.         return;
  74.     }
  75.  
  76.     binaryFile.seekg(recordNumber * sizeof(DictionaryEntry));
  77.     if (!binaryFile) {
  78.         cerr << "Error seeking in binary file!" << endl;
  79.         return;
  80.     }
  81.  
  82.     DictionaryEntry entry;
  83.     binaryFile.read(reinterpret_cast<char*>(&entry), sizeof(entry));
  84.     if (entry.englishWord[0] != '\0') {
  85.         cout << "Найдено: " << entry.englishWord.data() << " - " << entry.russianWord.data() << endl;
  86.     }
  87.     else {
  88.         cout << "Запись не найдена." << endl;
  89.     }
  90.     binaryFile.close();
  91. }
  92.  
  93. // Линейный поиск и вывод записи по ключу
  94. void BinaryFile::searchRecord(const string& binaryFileName, const string& key) {
  95.     ifstream binaryFile(binaryFileName, ios::binary);
  96.     if (!binaryFile.is_open()) {
  97.         cerr << "Failed to open binary file for search!" << endl;
  98.         return;
  99.     }
  100.  
  101.     DictionaryEntry entry;
  102.     while (binaryFile.read(reinterpret_cast<char*>(&entry), sizeof(entry))) {
  103.         if (strncmp(entry.englishWord.data(), key.c_str(), entry.englishWord.size()) == 0) {
  104.             cout << "Найдено (линейный поиск): " << entry.englishWord.data() << " - " << entry.russianWord.data() << endl;
  105.             return;
  106.         }
  107.     }
  108.  
  109.     cout << "Запись не найдена." << endl;
  110.     binaryFile.close();
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement