Search code examples
c++stringparsingtextdelimiter

Parsing a text file with a tab delimiter


I need to parse this text file that has essentially four strings separated by a tab. I've been researching and trying different methods and I can't quite figure out how to do it.

For example, I would need to parse this data:

Sandwiches (tab) Ham sandwich (tab) Classic ham sandwich (tab) Available (newline)

Sandwiches (tab) Cheeseburger (tab) Classic cheeseburger (tab) Not available (newline)

And I would read "sandwiches," "Ham sandwich," "Classic ham sandwich," and "available" all into separate variables for each line in the .txt file.

I've tried using string.find() and string.substr() to find the tab delimiter and then read the characters up until that delimiter but it all goes wrong and either throws an out of range error, prints nothing, or just prints the entire line into one variable.

Any help would be appreciated.

EDIT:

So I tried @john 's idea with the basic approach to test if it would work. I printed just category to see if it had read the right variable into it and it prints the whole file instead.

My Code:

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main() {
  ifstream inFS;
  string category, name, desc, avail;

  inFS.open("food.txt");

  if (!inFS.is_open()) {
    cout << "food.txt." << endl;
  }

  getline(inFS, category, '\t');
  getline(inFS, name, '\t');
  getline(inFS, desc, '\t');
  getline(inFS, avail, '\n');

  cout << category;
  //cout << name << " (" << category <<  ") " << " -- " << desc << " -- " <<
      //avail << endl;
  
  inFS.close();
  
  return 0;
}

Results from printing "category":

enter image description here


Solution

  • Simple way is

    getline(in, a, '\t');
    getline(in, b, '\t');
    getline(in, c, '\t');
    getline(in, d, '\n');
    

    where a, b, c, and d are std::string variables and in is your input stream.

    Better would be a little rudimentary error handling

    if (getline(in, a, '\t') && getline(in, b, '\t') && getline(in, c, '\t') && 
        getline(in, d, '\n'))
    {
        // ok
    }
    else
    {
        // something went wrong
    }