943,769 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2940
  • C++ RSS
Jan 24th, 2009
0

Open variable named files

Expand Post »
hello,,

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

here's my function:
Quote ...
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:
Quote ...
while (pent=readdir(pdir))
    {
        if (checkXML(pent->d_name))
        {
             list.push_back(pent->d_name);
	     string title("<TITLE>");
	     stringSearcher(pent->d_name, title);
        }
    }
sorry if you don't understand it,,

thx for helps... i need this asap btw,,
Last edited by azwraith69; Jan 24th, 2009 at 10:47 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
azwraith69 is offline Offline
21 posts
since Jul 2008
Jan 24th, 2009
3

Re: Open variable named files

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*.
Team Colleague
Reputation Points: 1135
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Jan 24th, 2009
0

Re: Open variable named files

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...
Quote ...
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,,
Reputation Points: 10
Solved Threads: 0
Newbie Poster
azwraith69 is offline Offline
21 posts
since Jul 2008
Jan 25th, 2009
0

Re: Open variable named files

sorry for double post...

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

thx for the help again..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
azwraith69 is offline Offline
21 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I use Dev-C++ but...
Next Thread in C++ Forum Timeline: There are some missing functions? Can you help me please!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC