Gistrec icon

StringContainer v1

Gistrec | PRO | 02/28/18 02:22:51 PM UTC | 0 ⭐ | 671 👁️ | Never ⏰ | []
C++ |

980 B

|

None

|

0 👍

/

0 👎

#pragma once
#include <string>
 
class StringContainer {
public:
    StringContainer(std::string &fileName, size_t count, size_t length) {
        fopen_s(&file_, fileName.c_str(), "r");
        count_ = count;
        length_ = length;
    }
 
    bool find(std::string &string) {
        return find(string, 0, count_);
    }
 
    bool find(std::string &string, size_t from, size_t to) {
        size_t now = (from + to) / 2;
        // WFT '\n' use 2 byte?!
        fseek(file_, now * (length_ + 2) * sizeof(char) , SEEK_SET);
        size_t index;
        char symbol;
        for (index = 0; index < length_; ++index) {
            fscanf_s(file_, "%c", &symbol, 1);
            if (string[index] != symbol) break;
        }
        if (index == length_) return true;
        if (now == from) return false;
        if (string[index] > symbol) {
            return find(string, now, to);
        } else {
            return find(string, from, now);
        }
    }
 
private:
    FILE* file_; // Файловый дискриптор
    size_t count_; // Кол-во строк
    size_t length_; // Длина строки
};

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎