How to skip over a few words to reach the last part of a string line?

Hello! Thank you in advance.

I'm a bit stuck right now - I have a file that reads like this:

b23p James Bond 2/19/17 5/20/20 5/21/20 11/27/20 FALSE Admin
o30z Monica Loi 5/18/14 3/23/20 4/24/20 11/27/20 FALSE Moderator

I'm reading the unique ID, the first name, the last name and I also want to only read the Privilege that the user has. I'm reading all these values into a list. Is there any way that I can just punch through the dates and reach the privilege? Or would I have to just create nodes(?) for everything instead of just the ones I need? The way I've written the code so far, it will read the first four points instead of just getting the privilege. Here's what I've written so far:

1
2
3
4
5
6
7
8
9
10
11
   void readData(List &L){
      ElementType id, first, last, priv; 
      ifstream fin; 
      fin.open("dadexcel.txt");


      while(!fin.eof()){
	id >> f >> sur >> gr; 
	L.insert(id, first, last, priv, L.getFirst());
      }
    }
Last edited on
I'm guessing you're using std::string.

You could read an entire line at a time getline(file, line) and then use http://www.cplusplus.com/reference/string/string/find_last_of/ to find the last instance of a space character in that line. The last word should be a substring from that position to the end of that line. Do a little while loop check that the substr actually contains text and you'll have it parsed. Here's an example without that final while loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

string getLastWord(string line)
{
    return line.substr(line.find_last_of(' '));
}

int main()
{
    string text = "I just want to output the last word.";
    cout << getLastWord(text);
}

output: word.


There are faster ways to read a whole file, one of which is to use a stringstream and read the whole file buffer file.rdbuf() into that. Then treat the stringstream as if it were the original file. (This means the whole file now exists in ram instead of reading a token at a time from the HD. This is a good move if the file is over 50Kb and less than 1Gb).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <sstream>

int main()
{
    ifstream inFile("filePath");
    stringstream ss;
    ss = inFile.rdbuf();
    string text;
    while(getline(ss, text))
    {
        // Do your file parsing here.
     }
    inFile.close();
}
Last edited on
Hello KittyIchigo1,

Try:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void readData(List &L)
{
    ElementType id, first, last, priv;
    std::string junk;

    ifstream fin;

    fin.open("dadexcel.txt");

    // <--- Should check that the file has opened and is usable.

    while (!fin.eof())  // <--- Does not work the way you are thinking.
    {
        fin >> id >> f >> sur >> junk >> junk >> junk >> junk >> junk >> gr;

        L.insert(id, first, last, priv, L.getFirst());
    }
}


Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool readData(List& L)
{
	ifstream fin ("dadexcel.txt");

	if (fin.is_open()) {
		ElementType id, first, last, priv;

		for (std::string junk; fin >> id >> first >> last >> junk >> junk >> junk >> junk >> junk >> priv; )
			L.insert(id, first, last, priv, L.getFirst());

		return true;
	}

	return false;
}


Assuming operator >> has been overloaded for type ElementType

Also function now returns bool to indicate success/failure of open. You could change to return the number of items read OK.

Last edited on
Topic archived. No new replies allowed.