Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "BinaryFileManagement.h"
- #include <fstream>
- #include <iostream>
- #include <sstream>
- void BinaryFile::createFromText(const string& textFileName, const string& binaryFileName) {
- ifstream textFile(textFileName);
- ofstream binaryFile(binaryFileName, ios::binary);
- if (!textFile.is_open() || !binaryFile.is_open()) {
- cerr << "Failed to open files!" << endl;
- return;
- }
- string line;
- while (getline(textFile, line)) {
- // Находим позицию первого пробела
- size_t pos = line.find(' ');
- if (pos != string::npos) {
- // Разделяем строку на английское и русское слово
- string englishWord = line.substr(0, pos);
- string russianWord = line.substr(pos + 1);
- DictionaryEntry entry(englishWord, russianWord);
- binaryFile.write(reinterpret_cast<char*>(&entry), sizeof(entry));
- }
- }
- textFile.close();
- binaryFile.close();
- }
- void BinaryFile::addRecord(const string& binaryFileName, DictionaryEntry& entry) {
- ofstream binaryFile(binaryFileName, ios::binary | ios::app);
- if (!binaryFile.is_open()) {
- cerr << "Failed to open binary file!" << endl;
- return;
- }
- binaryFile.write(reinterpret_cast<char*>(&entry), sizeof(entry));
- binaryFile.close();
- }
- void BinaryFile::removeRecord(const string& binaryFileName, const string& key) {
- ifstream binaryFile(binaryFileName, ios::binary);
- ofstream tempFile("temp.bin", ios::binary);
- if (!binaryFile.is_open() || !tempFile.is_open()) {
- cerr << "Failed to open files!" << endl;
- return;
- }
- DictionaryEntry entry;
- while (binaryFile.read(reinterpret_cast<char*>(&entry), sizeof(entry))) {
- if (strncmp(entry.englishWord.data(), key.c_str(), entry.englishWord.size()) != 0) {
- tempFile.write(reinterpret_cast<char*>(&entry), sizeof(entry));
- }
- }
- binaryFile.close();
- tempFile.close();
- // Переименовываем tempFile в binaryFileName
- remove(binaryFileName.c_str());
- rename("temp.bin", binaryFileName.c_str());
- }
- void BinaryFile::readRecord(const string& binaryFileName, int recordNumber) {
- ifstream binaryFile(binaryFileName, ios::binary);
- if (!binaryFile.is_open()) {
- cerr << "Failed to open binary file!" << endl;
- return;
- }
- binaryFile.seekg(recordNumber * sizeof(DictionaryEntry));
- if (!binaryFile) {
- cerr << "Error seeking in binary file!" << endl;
- return;
- }
- DictionaryEntry entry;
- binaryFile.read(reinterpret_cast<char*>(&entry), sizeof(entry));
- if (entry.englishWord[0] != '\0') {
- cout << "Найдено: " << entry.englishWord.data() << " - " << entry.russianWord.data() << endl;
- }
- else {
- cout << "Запись не найдена." << endl;
- }
- binaryFile.close();
- }
- // Линейный поиск и вывод записи по ключу
- void BinaryFile::searchRecord(const string& binaryFileName, const string& key) {
- ifstream binaryFile(binaryFileName, ios::binary);
- if (!binaryFile.is_open()) {
- cerr << "Failed to open binary file for search!" << endl;
- return;
- }
- DictionaryEntry entry;
- while (binaryFile.read(reinterpret_cast<char*>(&entry), sizeof(entry))) {
- if (strncmp(entry.englishWord.data(), key.c_str(), entry.englishWord.size()) == 0) {
- cout << "Найдено (линейный поиск): " << entry.englishWord.data() << " - " << entry.russianWord.data() << endl;
- return;
- }
- }
- cout << "Запись не найдена." << endl;
- binaryFile.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement