Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting this specific error for this specific part of my code. Two other places as well, but I think if I can solve one I can solve the others. This error is for this part of the code, "ins.open(fileName.c_str()) ;"

void openInfile (ifstream& ins)
{
	string fileName = " " ;
	ofstream outs ;		// outs is an output stream
	cout << "Enter file of madlibs outline." << endl ;
	cin >> fileName ;
	ins.open(fileName.c_str()) ;		//connects ins to file inFile
	if (ins.fail (fileName.c_str()))
	{
		cerr << "Error: Unable to open file : FILENAME" << endl ;
	}
	else
	{
		openOutfile (outs) ;
	}
}


What I have tried:

I have asked many people to help me and have researched through many similar questions, but I still can't find anything to help me.
Posted
Updated 28-Nov-17 21:47pm

Change from
Quote:
if (ins.fail (fileName.c_str()))
To
C++
if ( ins.fail() )
 
Share this answer
 
Comments
Member 13479017 29-Nov-17 7:51am    
Thank you, but it still does not work.
CPallini 29-Nov-17 7:57am    
Does it compile? If it doesn't compile: what is the (exact) error message?
Member 13479017 29-Nov-17 8:08am    
okay, so with that specific line of code, the error message reads:

prog5B.cpp: In function ‘void openInfile(std::ifstream)’:
prog5B.cpp:50: error: could not convert ‘ins.std::basic_ios<_CharT, _Traits>::fail [with _CharT = char, _Traits = std::char_traits<char>]’ to ‘bool’
CPallini 29-Nov-17 8:17am    
pay attention to the number of round braces, it is
if ( ins.fail() )
Your problem is in
if (ins.fail (fileName.c_str()))
You just need
if (ins.fail)
and you will probably want to change
cerr << "Error: Unable to open file : FILENAME" << endl ;
to something like
cerr << "Error: Unable to open file : " << filename.c_str() << endl ;
 
Share this answer
 
v2
Comments
CPallini 29-Nov-17 3:48am    
Hey Peter, fail is a method.
Peter_in_2780 29-Nov-17 5:08am    
Oops! My fossilised brain. Thanks for the correction, Carlo.
Member 13479017 29-Nov-17 7:51am    
Thank you. I changed it, but it still does not work. Now the error at that spot changed from "no matching function for call to" to "could not convert"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900