Open variable named files

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 21
Reputation: azwraith69 is an unknown quantity at this point 
Solved Threads: 0
azwraith69's Avatar
azwraith69 azwraith69 is offline Offline
Newbie Poster

Open variable named files

 
0
  #1
Jan 24th, 2009
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>");
	     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.
I am living in a mere program...
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,047
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Open variable named files

 
3
  #2
Jan 24th, 2009
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*.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 21
Reputation: azwraith69 is an unknown quantity at this point 
Solved Threads: 0
azwraith69's Avatar
azwraith69 azwraith69 is offline Offline
Newbie Poster

Re: Open variable named files

 
0
  #3
Jan 24th, 2009
Originally Posted by Rashakil Fol View Post
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,,
I am living in a mere program...
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 21
Reputation: azwraith69 is an unknown quantity at this point 
Solved Threads: 0
azwraith69's Avatar
azwraith69 azwraith69 is offline Offline
Newbie Poster

Re: Open variable named files

 
0
  #4
Jan 25th, 2009
sorry for double post...

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

thx for the help again..
I am living in a mere program...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC