Link to home
Start Free TrialLog in
Avatar of prain
prainFlag for United States of America

asked on

Text File Read Question

Hello,
I have a humongous text file with elements like this... (This is an example only and NOT the real file)...Note that in each item... First half before the underscore is the major item name and the other half is the minor descriptions.

item1_data1
item1_data2
item1_data3
.............
item2_data1
item2_data2
item2_data3
.............
item3_data1
item3_data2
item3_data3
............

There are about 75000 items in this file.

I am writing a C++ class to pickup only the major name of each item. I.E. My result from the above humongous file should be...

item1
item2
item3
.......

I know thare are tons of techniques out there. What is the real efficient method I should use so that my result is produced in nano seconds :-) (serioulsly efficiency is extreamly important for me)
Avatar of jkr
jkr
Flag of Germany image

You'll have to read the entire file anyway, so there's not much room for improvement. The simplest way I can imagine would be to

#include <fstream>
#include <string>
#include <list>

using namespace std;

//...

string line;
size_t pos;
list<string> items;
ifstream is("file.txt");

if (!is.is_open()) {

  // error, no such file
}

while (!is.eof()) {

  getline(is,line);

  if (string::npos == (pos = line.find('_'))) {

    // error, malformed line w/o underscore
  }

  items.push_back(line.substr(0,pos));
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of prain

ASKER

Thanks