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ï

Nick Evan commented: Good first post +12

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;
}

No, just read the file one line at a time.

Member Avatar for iamthwee

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?

Member Avatar for iamthwee

Something like that, post a working example of your code.

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

Member Avatar for iamthwee

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.

commented: Why is .eof() wrong? The syntax is correct. Sould we just believe you? Maybe if you'd explain... -2
Member Avatar for iamthwee
#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();

}

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

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

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 :)

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.