Jun 30, 2019 at 2:30pm UTC
(Im sorry if u saw any english erros becouse english is not my main language)
Hey Guys, I recently made a function to read a file, but she has a problem that i cant solve. The error appears on "ifstream".
Can anyone helpme solve that problem, thak you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include <iostream>
#include <fstream>
using namespace std;
void readFile(string input)
{
string finalString, txt = ".txt" , mark = "\"" , writer;
finalString = mark + input + txt + mark;
ifstream filename(finalString); // error apperas here
if (filename.is_open())
{
while (getline(filename,writer))
{
cout << writer << endl;
}
filename.close();
}
else
{
cout << "Fail opening " << finalString << endl;
}
}
int main()
{
string file;
cin >> file;
readFile(file);
}
Last edited on Jun 30, 2019 at 2:31pm UTC
Jun 30, 2019 at 2:38pm UTC
But it keeps giving me error.
Jun 30, 2019 at 2:39pm UTC
What is the text of the error message?
Jun 30, 2019 at 2:43pm UTC
error : no matching function for call to "std::basic_ifstream<char>::basic_ifstream...
Jun 30, 2019 at 3:21pm UTC
Now it appears another problem, I rewrited the code I but cant open a file that I created.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void readFile(string input)
{
string finalString, txt = ".txt" , mark = "\"" , writer;
finalString = mark + input + txt + mark;
ifstream filename;
filename.open(finalString.c_str() );
if (filename.is_open())
{
while (getline(filename,writer))
{
cout << writer << endl;
}
filename.close();
}
else
{
cout << "ERROR : CANNOT OPEN " << finalString;
}
}
int main()
{
string file;
cin >> file;
readFile(file);
}
Last edited on Jun 30, 2019 at 3:22pm UTC
Jun 30, 2019 at 3:55pm UTC
Hello troyaan,
Try this: finalString = input + txt;
. You do not need to put the string in ("")s.
Andy
Edit: typo
Last edited on Jun 30, 2019 at 3:56pm UTC