Hello, i'm new to daniweb and also quite new to C++. I'm trying to write a function that takes a filename and a string query like

string GetStringValue(string filename, string query)
{
/*...*/
}

then finds the query in the file, and returns what's behind the = mark.

the file's entries are like this:

ProgramTitle="This is the program's Title"
MyName="Katmai539"

so when i call the function like

string asimplestring=(GetStringValue("file.txt", "ProgramTitle"));

it has to return "This is the program's Title" as a string.

Searching the webs left me reading about putting the whole textfile in buffers and then buffer.find(query) them, but it made me a bit unsure about doing this.
Do i really need to put the whole file in a buffer just to find a string in it?

Many, many thanks,

Katmaï

Comments
Good first post

by the way, i understand the fstream-functions like ifstream, i got

using namespace std;

string GetStringValue(char* filename, string query)
{
	ifstream file;

	file.open(filename, fstream::in);

	string StringValue=/* ... */;

	return StringValue;
}

Edited 6 Years Ago by katmai539: n/a

Yes follow that advice then:-

ProgramTitle="This is the program's Title"

Assuming there are no extra '=' signs within the actual quotation you could use the '=' to split the string into two parts. The first part being the reference, the second part being the contents.

no there are no extra =signs in the textfile.

So i could use the = as a delimiter, then when query==file.getline(blah,256,'=') it should break a loop or something?

Edited 6 Years Ago by katmai539: too many

currently i have this:

using namespace std;

string GetStringValue(char* filename, string query)
{
	string StringValue;
	string tmp;
	size_t found;
	
	ifstream file;
	file.open(filename, fstream::in);

	while (!file.eof())
	{
		file.getline(tmp);
		found=tmp.find(query,0);
		if (found!=string::npos)
			/* extraction code here */
	}
	
	file.close();

	return StringValue;
}

but i still don't know how to extract things from string tmp after the =mark. could someone point me in the right direction?

thanks in advance :)

find return a pointer to the location of the first =.
Then copy the contents from after the = till end of string into a temporary buffer
Return the temporary buffer

currently i have this:

using namespace std;

string GetStringValue(char* filename, string query)
{
	string StringValue;
	string tmp;
	size_t found;
	
	ifstream file;
	file.open(filename, fstream::in);

	while (!file.eof())
	{
		file.getline(tmp);
		found=tmp.find(query,0);
		if (found!=string::npos)
			/* extraction code here */
	}
	
	file.close();

	return StringValue;
}

but i still don't know how to extract things from string tmp after the =mark. could someone point me in the right direction?

thanks in advance :)

The easiest way would be to use '=' as a delimiter. This would create two parts, your reference and the contents. I'll post an example. Also don't use !file.eof() . It's wrong. . . Again I'll post an example.

Edited 6 Years Ago by iamthwee: n/a

Comments
Why is .eof() wrong? The syntax is correct. Sould we just believe you? Maybe if you'd explain...
#include <iostream>
#include <string>
#include <vector>
#include <sstream>

int main()
{

   std::string tmp = "ProgramTitle=\"This is the program's Title\"";

   std::vector <std::string> array;
   std::string token;

   std::istringstream iss ( tmp );

   while ( getline ( iss, token, '=' ) )
   {
      array.push_back ( token );
   }
   
   std::cout << array[1] << std::endl; //reference
   std::cout << array[2] << std::endl; //contents
   
   std::cin.get();

}

Edited 6 Years Ago by iamthwee: n/a

The length of the search string points at the '='. Use that as a guide.

Comments
Repetive advice kiddo, read before you post. Oh and just have faith. . . believe me :)

Okay i've never worked with vectors before (against all recommendations of my fellow programmers) and stringstreams weren't in my code from the beginning. the code i currently have,

string GetStringValue(char* filename, string query)
{
	string StringValue;
	string tmp;
	size_t found;
	
	ifstream file;
	file.open(filename, fstream::in);

	while(!file.eof())
	{
		getline(file,tmp);
		while((found=tmp.find(query,0)))
		{
			StringValue=tmp.substr(tmp.find(query,0)+1);
		}
	}

	file.close();

	return StringValue;
}

still has !=file.eof() and i see it's wrong because the code compiles but the program hangs. I'm going to grab a beer now, will continue tomorrow :) many, many thanks :)

It's working! Currently it still doesn't error handling but that's the next step!
Working code here: (while using namespace std;)

string GetStringValue(char* filename, string query)
{
	string StringValue;
	string tmp;
	size_t pos;
	
	ifstream file;
	file.open(filename, fstream::in);

	while(!file.eof())
	{
		while (getline(file,tmp))
		{
			pos=tmp.find(query,0);
			if (pos!=string::npos)
			{
				StringValue=tmp.substr(pos+query.length()+1);
			}
		}
	}

	file.close();

	return StringValue;
}

It still uses (!file.eof()) and i didn't state = as a delimiter nowhere, instead i just add 1 to the string::substr parameter.

Thank you guys, next step is error handling :)

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