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?

Dani AI

Generated

Nice progress — was right to suggest locating "MATRIX", and got things working. For a more robust, maintainable solution consider reading up to the semicolon and then parsing only the chunk that contains "MATRIX". That avoids brittle eof-based loops and handles newlines inside the irrelevant sections.

A simple, safe pattern:

  • Use std::getline with ';' as delimiter to read a chunk up to each semicolon.
  • Search that chunk for the literal "MATRIX". If found, take the substring after "MATRIX" (this handles cases like MATRIXNameA where the prefix is attached).
  • Trim leading whitespace from the substring and feed it to std::istringstream.
  • Extract name/number pairs from the stream until it runs out, validating numbers as needed.

Example (illustrative):

#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <utility>

// open file and check
std::ifstream in("test.txt");
std::string chunk;
while (std::getline(in, chunk, ';')) {
    auto pos = chunk.find("MATRIX");
    if (pos == std::string::npos) continue;
    std::string rest = chunk.substr(pos + 6); // skip "MATRIX"
    // trim leading space
    size_t s = rest.find_first_not_of(" \t\r\n");
    if (s != std::string::npos) rest = rest.substr(s);
    std::istringstream iss(rest);
    std::vector<std::pair<std::string,std::string>> pairs;
    std::string name, num;
    while (iss >> name >> num) pairs.emplace_back(name, num);
    // use pairs...
    break;
}

Notes and edge cases: handle file-open failures, consider case-insensitive search if "matrix" can vary, validate numeric tokens (e.g., std::stoi with try/catch), and think about multiple "MATRIX" occurrences. Reading by semicolon is memory-friendly and keeps parsing logic simple.

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.