I have been stuck on this problem for a while, and I just can't figure it out. I'm sure there's an incredibly easy way to do it, but I'm at a loss...

I need to take in this text file:

Smith, M.N., Martin, G., Erdos, P.: paper name
Erdos, P., Reisig, W.: paper2 name
Smith, M.N., Chen, X.: paper3 name
Jablonski, T., Hsueh, Z.: paper4 name
Hsueh, Z.
Chen, X.
Smith, M.N.

The problem I have is that I can't get the file input right. I need it to stop at the ':', because the name of the paper isn't important to my algorithm.

struct Name {
	string lastName;
	string firstName;

int main()
	Name n;
        int num, P, V;
	vector<Name> v1;
	vector<Name>::iterator it;
	string paper;
	fstream myfile;
	myfile >> num >> P >> V;
	while (!myfile.eof())
		myfile >> n.lastName >> n.firstName;
	for (it=v1.begin();it!=v1.end();it++)

    return 0;

I want to pass the 'Name' structure into a vector to compare, but I want only the last and first name. No commas.

Essentially, my problem is that I want to read the file up to the ':' and then ignore the rest. I feel like I've tried everything, but I just can't make it happen.

Any help is greatly appreciated!

7 Years
Discussion Span
Last Post by twsmale

You can use the getline() function. Notice in the link, that there is a parameter called delim which stands for a character that delimits your reading operation (it will stop reading in the string when it finds that character). By default, the delimiter is the newline character (and thus, the name "getline()"). To read, up to a comma, you can do this:

getline(myfile, n.lastname, ',');

But, since you can also stop at a colon, you will have to first extract up to the colon, and then extract the individual names from that string. That can be done with a stringstream object. As so, for example:

string str_up_to_colon;
stringstream ss(str_up_to_colon);
while(!ss.eof()) {
  getline(ss, n.lastname, ',');
  getline(ss, n.firstname, ',');

This might not work for all the cases that you have, but the examples above give you the basic tool set to use. If all fails, you can always use the myfile.get() function to extract each character one by one and construct your strings one character at a time.


I love the answer, and that's exactly what I was looking for...but I can't get the getline() function to work. I think the syntax is wrong.

I would have thought it was "myfile.getline(....)" but that also pops an error. Also, for the stringstream, I get an error that says "incomplete type is not allowed" at the following line:

stringstream ss(str_up_to_colon);

You have to include the proper headers. For stringstream, it is #include <stringstream>. And for the getline, you need to include #include <string>. And it is not a mistake, the getline is not the one in the file stream class, it is a special global function that is defined in <string> to allow for string input from a stream.


Ah! I'm an idiot...I keep forgetting the difference between single and double quotes. That was my problem the whole time. Wow, works perfectly now.


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.