1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <vector>
//ID#Hours#Minutes#Seconds#MinValue#MaxValue#Comments
struct Item {
std::string id;
int hours {};
int min {};
int sec {};
int minval {};
int maxval {};
std::string comm;
};
int main()
{
constexpr char delim {'#'};
const std::string fnam {"bigfile.txt"};
std::ifstream ifs(fnam);
if (!ifs.is_open())
return (std::cout << "Cannot open input file\n"), 1;
std::string data;
ifs.seekg(0, std::ios_base::end);
data.resize(static_cast<size_t>(ifs.tellg()));
ifs.seekg(0, std::ios_base::beg);
ifs.read(data.data(), data.size());
std::vector<Item> items;
items.reserve(std::count(data.begin(), data.end(), '\n') + 1);
for (auto tokenStart = 0U, delimPosition = data.find('\n'); delimPosition != std::string::npos; delimPosition = data.find('\n', tokenStart)) {
const auto d {data.find(delim, tokenStart + 1)};
Item itm;
char* ed {};
//itm.id = data.substr(tokenStart + 1, d - tokenStart - 1);
itm.id = data.substr(tokenStart, d - tokenStart);
itm.hours = strtol(data.data() + d + 1, &ed, 10);
itm.min = strtol(ed + 1, &ed, 10);
itm.sec = strtol(ed + 1, &ed, 10);
itm.minval = strtol(ed + 1, &ed, 10);
itm.maxval = strtol(ed + 1, &ed, 10);
//itm.comm = data.substr(ed - data.data() + 1, delimPosition - 2 - (ed - data.data()));
itm.comm = data.substr(ed - data.data() + 1, delimPosition - 1 - (ed - data.data()));
items.emplace_back(itm);
tokenStart = ++delimPosition;
}
for (const auto & itm : items)
std::cout << itm.id << " " << itm.hours << " " << itm.min << " " << itm.sec << " " << itm.minval << " " << itm.maxval << " " << itm.comm << '\n';
}
|