Well, I'm working on a simple project but I've hit a snag in my progress so far. I'd like to take a file that has text which will be something similar to:

Placemarker 1       = words/numbers
Placemarker 2       = words/numbers
Placemarker 3       = words/numbers

so you'll have a file like:

Name        = Bob
Job         = Student
Whatever    = stuff

Name        = John
Job         = Student
Whatever    = things

The place markers are just for readabilty of the resulting text file.

So I'd like to know how to use C++ (maybe fstream?) to:
a. check for the placemarker
b. if the marker is the one I want (like "Name") skip to the "= " and take the value after that to be placed in a temp variable for checking.
c. See if the variable (such as "Bob") matches the one I want
d. Then repeat the process so many times line by line (to maybe input into a structure of values) until it hits the blank line and will then stop.

So the user could type in "Bob", the set text file "whatever.txt" would be opened, name lines would be found, then checked for "Bob", then input the data if matched.

I (obviously) don't know enough about C++ I/O and fstream to figure this out, but I am already thinking that a set number of characters (include spaces) between the start of the marker ("|Name") and the end of the = thing ("= |") should be a set number of characters.

Any help/suggestions/links as to how to go about this would be much appreciated.

strings or c-strings? You need to give us more information on how your program currently is written.

it won't matter how many spaces are between the marker and the '=' symbol. After reading the whole line into memory take the first word (which starts with the first space on the line). If its the marker you want search for the '=' symbol and the text you want follows that (ignoring the spaces between '=' and the text).

it won't matter how many spaces are between the marker and the '=' symbol. After reading the whole line into memory take the first word (which starts with the first space on the line). If its the marker you want search for the '=' symbol and the text you want follows that (ignoring the spaces between '=' and the text).

That sounds just like what I wanted!
Now for follow up questions tough: How do I search through the line? Am I going to use a simple ifstream to skip the whitespaces and store as strings? Am I going to use the entire line string and cut it up somehow? If I do use ifstream, how can I make it handle values with spaces (like if the name was "Bob Jones" instead of just "Bob"). Thanks for your help.

strings or c-strings? You need to give us more information on how your program currently is written.

Well, I would prefer to use strings and not c-strings, but that isn't important. Also, I did not include any real code because this part of the project is completely independent from the rest. This code will essentially be just a function or whatever that is called in the start to load up different sets of information from pre-set files.

first, use std::string's find() method to find the first space, then use its substr() method to extract the first word

std::string line = "name    =  John";
std::string tag;

std::string::size_type pos = line.find(' ');
if(pos != std::string::npos)
{
   // first space found.  So extract the first word
   tag = line.substr(0,pos);
}

Now, use code similar to above to find the '=' symbol, and extract everything after that. You will also want to skip all the spaces between '=' and the first non-white-space character.

first, use std::string's find() method to find the first space, then use its substr() method to extract the first word

std::string line = "name    =  John";
std::string tag;

std::string::size_type pos = line.find(' ');
if(pos != std::string::npos)
{
   // first space found.  So extract the first word
   tag = line.substr(0,pos);
}

Now, use code similar to above to find the '=' symbol, and extract everything after that. You will also want to skip all the spaces between '=' and the first non-white-space character.

Oh, now I see what you meant. Thanks again for the help.

This question has already been answered. Start a new discussion instead.