简体   繁体   中英

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++.

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
}

You should read file line by line, and for each line use string's substr() method to parse a line based on space location, and you can use find() method to find the location of delimiter. take the word part which is before space and ignore rest.

You can look here for an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM