Advertisement
Gistrec

StringContainer v1

Feb 28th, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #pragma once
  2. #include <string>
  3.  
  4. class StringContainer {
  5. public:
  6.     StringContainer(std::string &fileName, size_t count, size_t length) {
  7.         fopen_s(&file_, fileName.c_str(), "r");
  8.         count_ = count;
  9.         length_ = length;
  10.     }
  11.  
  12.     bool find(std::string &string) {
  13.         return find(string, 0, count_);
  14.     }
  15.  
  16.     bool find(std::string &string, size_t from, size_t to) {
  17.         size_t now = (from + to) / 2;
  18.         // WFT '\n' use 2 byte?!
  19.         fseek(file_, now * (length_ + 2) * sizeof(char) , SEEK_SET);
  20.         size_t index;
  21.         char symbol;
  22.         for (index = 0; index < length_; ++index) {
  23.             fscanf_s(file_, "%c", &symbol, 1);
  24.             if (string[index] != symbol) break;
  25.         }
  26.         if (index == length_) return true;
  27.         if (now == from) return false;
  28.         if (string[index] > symbol) {
  29.             return find(string, now, to);
  30.         } else {
  31.             return find(string, from, now);
  32.         }
  33.     }
  34.  
  35. private:
  36.     FILE* file_; // Файловый дискриптор
  37.     size_t count_; // Кол-во строк
  38.     size_t length_; // Длина строки
  39. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement