SoloNasus SoloNasus - 1 month ago 13
C++ Question

How to efficiently read only strings from a big txt file

I have a very big .txt file (9 MB). In it the words are stored like this :

да 2337093
е 1504540
не 1480296
се 1212312


Every line in the .txt file consists of a string followed by a single space and a number.

I want to get only the words and store them in a string array. I see that a regex will be an overkill here, but fail to think of a another way as i'm not familiar with streams in c++.

Answer

Similar to below sample

#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<string> strings;
    ifstream file("path_to_file");
    string line;
    while (getline(file, line))
        strings.push_back(line.substr(0, line.find(" ")));

    // Do whatever you want with 'strings' vector
}
Comments