0

I am working on a client/server program using sockets and I am trying to parse the input file. I have a struct to store the Majors, the early career pay, and the mid-career, respectively. The client program asks the user to input the name of the major and the server program returns both salaries.

The issue with the input file is this:

Geophysics 54100 122200

Cognitive Science 54000 121900

Electrical Power Engineering 68600 119100

They are all separated as Major[TAB]Pay[Tab]Pay, and the majors have spaces in them. I want to store each of them in the struct.

Any solution to this?

IronSage
  • 1
  • 8
  • 4
    Hint: `std::getline(input_file, text, '\t')`. – Thomas Matthews Apr 05 '18 at 19:26
  • Possible duplicate of https://stackoverflow.com/questions/236129/the-most-elegant-way-to-iterate-the-words-of-a-string – Galik Apr 05 '18 at 19:45
  • Possible duplicate of [how to split a file with space and tab differenciation](https://stackoverflow.com/questions/10617094/how-to-split-a-file-with-space-and-tab-differenciation) – xskxzr Apr 06 '18 at 04:42

2 Answers2

1

You can use the third argument in getline() to say what character to stop at. The default is \n, but you can also specify that it is \t to have it stop at the tab you want:

getline(std::cin, line, '\t');
Hawkeye5450
  • 672
  • 6
  • 18
1

Start with something this:

ifstream f("c:\\temp\\test.txt");
string s;
while (getline(f, s))
{
    istringstream iss(s);
    string major, early, mid;
    getline(iss, major, '\t');
    getline(iss, early, '\t');
    getline(iss, mid, '\t');
    cout << major << '|' 
         << early << '|' 
         << mid   << endl;
}
Sid S
  • 6,037
  • 2
  • 18
  • 24
  • So that prints out everything except the first letter of every major. If I remove the space from the second getline, I get this: Geophysics54100 122200 – IronSage Apr 06 '18 at 17:36
  • This is what I get as output: Major: Petroleum Early: Mid: Major: Engineering Early: 94600 Mid: Major: 175500 – IronSage Apr 06 '18 at 21:58
  • But I have to push the data into the struct. I was just using the cout to see if it's being split and stored the way I want it to. – IronSage Apr 07 '18 at 04:46