0

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

  • have you tried `getline()`? – James Sep 21 '22 at 20:46
  • 1
    You could make something with find and substr work but If you want your bad code fixing, then you have to post it. Just post your best effort and someone will tell you what is wrong. – john Sep 21 '22 at 20:47
  • @john I made some edits based on your answer and posted the code. – VortexX Bolt Sep 21 '22 at 21:36
  • @VortexXBolt The only explanation is that your file is not tab delimited, which would also explain why you could not get the find and substr solution to work either. – john Sep 22 '22 at 05:40
  • @VortexXBolt It certainly looks tab delimited, but I'm guessing that at some point the tabs got replaced by spaces. – john Sep 22 '22 at 05:44
  • @john Yeah I figured out I had copied the text into my compiler and the copying process changed the tabs to spaces. I implemented your method after I fixed it and it worked. Thanks for your help. – VortexX Bolt Sep 23 '22 at 23:30

1 Answers1

2

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
}
john
  • 85,011
  • 4
  • 57
  • 81