954,153 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Open variable named files

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>");
<strong>stringSearcher(pent->d_name, title);</strong>
}
}

sorry if you don't understand it,,

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

azwraith69
Newbie Poster
21 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

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*.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 176
 

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,,

azwraith69
Newbie Poster
21 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

sorry for double post...

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

thx for the help again..

azwraith69
Newbie Poster
21 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You