String parsing help

I have a text file that goes:

"blah blah blah blah, TEAMNAME, blah blah blah blah blah..."

I know how to use getline, but it returns the whole paragraph. I need it to JUST grab the teamname, but since that variable changes every time, I'm not sure how to do that. Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

    string name = "Thank you";
    string gotName;
    string test;
    size_t found;
    ifstream testFile("E:\\emails\\IQA039.txt");
    ofstream outputFile("E:\\outputIQA039TEST.txt", std::ios::app);

int main()
{
    if (testFile.is_open())
    {
        while ( testFile.good() )
        {
            getline(testFile, test);
            found = test.find(name);
            if (found!=string::npos)
            {
                cout << test << endl;
                //Does it maybe have something to do with 'endl'?
            }

        }

    }
    else
    {
        cout << "Error opening file to read from";
    }
    return 0;
}
For this type of parsing you'll need to determine how to recognize the team name which means finding a pattern in your input file.

If it is always the 5th word in a line you may want to do this:
1
2
3
4
5
6
7
8
std::string temp;
getline(testFile, test);
std::stringstream iss(test);
iss >> temp; // ignore word1
iss >> temp; // ignore word2
iss >> temp; // ignore word3
iss >> temp; // ignore word4;
iss >> teamname;


if it is a word that belongs to a limited vocabulary you may want to do this:
1
2
3
4
5
6
getline(testFile,test);
while (teamname != "trojans" || teamname != "gladiators" || !iss.good())
{
    std::stringstream iss(test);
    test >> teamname;
}

FIXED - It runs into this error at line 30: "error: variable 'std::stringstream iss' has initializer but incomplete type"

Unfortunately I haven't been able to find anything that makes sense to me so I can fix this. It appears that iss is not a function and I need to declare it somehow, somewhere? EDIT - I added <sstream> and now it compiles

Here's the code, with your part added in..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

    string teamname;
    string test;
    ifstream testFile("E:\\emails\\IQA039.txt");
    ofstream outputFile("E:\\outputIQA039TEST.txt", std::ios::app);

int main()
{
    if (testFile.is_open())
    {
        while ( testFile.good() )
        {

            std::string temp;
            getline(testFile, test);
            std::stringstream iss(test);
            iss >> temp; // ignore word1
            iss >> temp; // ignore word2
            iss >> temp; // ignore word3
            iss >> temp; // ignore word4
            iss >> temp; // ignore word5
            iss >> temp; // ignore word6
            iss >> teamname; //The teamname is always the 7th word and always comes right after a comma

            cout << test << endl;

        }

    }
    else
    {
        cout << "Error opening file to read from";
    }
    return 0;
}


EDIT: It compiles now, but it outputs the entire file..
Last edited on
In line 31, replace cout << test << endl; with cout << teamname << endl;
I've tried cout << test << endl; and cout << teamname << endl; and no matter how I do it, it prints a lot of information I don't need or want. The teamname output is just a single, organized column compared to the test output. Does anyone know of a webpage I should be looking at to help me? I can't seem to find anything relevant to my problem anywhere...


EDIT: I believe the problem is that it prints the 7th word of every line, based on comparing my output results to my original text file. The files I am reading from are emails converted to text files and I can provide an example if anyone thinks it would be helpful.
Last edited on
Topic archived. No new replies allowed.