I'm trying to make a program that will read names and numbers from a file, but I'm quite inexperienced in C++ in general, and most tutorials and help threads I read tend to make me confused.

So far, I have used a test file with a text like "NameA 01234 NameB 11111 NameC 55555". I want the names in one string and the numbers in another one, which I have managed to do with the code:

ifstream in("test.txt");
	string inName[3], inChar[3];
	for (int i= 0; i<3; i++)
		in >> plant[i].name >> plant[i].charString

My problem now is that the file I actually want to read from is more complicated, and looks more like:
"LotsOfTextThat;IAmNotInterestedIn; MATRIXNameA 01234 NameB 11111 NameC 55555; MoreTextThatIsOfNoUse".
In other words, I want to start reading after the word "MATRIX" and stop at the character ";". How do I edit my code to make it do that?

Recommended Answers

All 3 Replies

I just saw that I messed up a bit. The code was supposed to be like this:

ifstream in("test.txt");
	string inName[3], inChar[3];
	for (int i= 0; i<3; i++)
		in >> inName[i] >> inChar[i]

and what I want to do is to start reading after the word "MATRIX" and stop before the character ";"

And Secondly. Why dont you just search for the word Matrix in Your File?

string inName[3],inChar[3];
while(in.eof())
string s;
in>>s;
if(s=="matrix")
{
	for (int i= 0; i<3; i++)
{
		in >> inName[i] >> inChar[i];
}
break;
}
else 
//Do nothing 
}

That way You will read the next words into strings

Well, that's just about the help I wanted :)

As I said, my programming skills are not too great. After a while, I figured out that you must have meant "while(!in.eof())", and now my program works perfectly fine.

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.