0

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

Teodor Dyakov
  • 344
  • 2
  • 13
  • 1
    Nine megabytes is not very big. Just open it, use `std::getline()` to read each line, and `find()` to locate the delimiting space. If you are guaranteed that the contents of the file will always be well-formed, then you might as well just use `operator>>` – Sam Varshavchik Oct 22 '16 at 14:29

2 Answers2

0

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
}
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
-1

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.

Community
  • 1
  • 1
Ramesh Karna
  • 816
  • 1
  • 7
  • 24