hello,,

i need help on how to open files by using variable names..

here's my function:

void stringSearcher(string filename, string search)
{
	ifstream file;
	file.open(filename);
	if(!file.is_open())
	{
		cerr<<"File can't be open..."<<endl;
		exit(1);
	}
	string line;

	while(!file.eof())
	{
		getline(file, line);
		if(line.find(search)==0)
		{
			displayTitle(line);
			exit(1);
		}
	}
}

my problem is that there's always an error on the file.open line...
"no matching function to call...etc"

this is where i call the function in my main:

while (pent=readdir(pdir))
    {
        if (checkXML(pent->d_name))
        {
             list.push_back(pent->d_name);
	     string title("<TITLE>");
	     [B]stringSearcher(pent->d_name, title);[/B]
        }
    }

sorry if you don't understand it,,

thx for helps... i need this asap btw,,

Recommended Answers

All 3 Replies

When you see it say that there's "no matching function", that means there's no function with the given name that expects the types you've provided. If you double-check the documentation for ifstream's open function, you'll see that it takes a char*, not a string.

So use the .c_str() member function to get a char*.

When you see it say that there's "no matching function", that means there's no function with the given name that expects the types you've provided. If you double-check the documentation for ifstream's open function, you'll see that it takes a char*, not a string.

So use the .c_str() member function to get a char*.

thx,, for quick reply,, :) i have just did that..

now,, there's no more errors,,

but the problem is,, File can't be open...

void stringSearcher(string filename, string search)
{
	ifstream file;
	ifstream openfile(filename.c_str());
	if(!file.is_open())
	{
		cerr<<"File can't be open..."<<endl;
		exit(1);
	}

	string line;
	while(!file.eof())
	{
		getline(file, line);
		if(line.find(search)==0)
		{
			displayTitle(line);
			exit(1);
		}
	}
}

when i cout the filename,, it is right,, for example -> "file.xml"
and it is in the same directory with my source codes..
but it can't be open using .c_str()..

if i use file.open("file.xml"),, it opens properly....

what's the problem??

thx again,,

sorry for double post...

i have already fixed my problem... just overlooked something..

thx for the help again..

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.