I'm trying to make a program where the user enters a file name and then the program tries opening it and checks to see if it is open. I am using the getline function. Here is my code so far:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void readGameFile();
int main()
{
readGameFile();
return 0;
}
void readGameFile()
{
ifstream infile;
string s;
string fileName;
getline(cin,fileName);
infile.open(fileName);
if(infile.is_open())
{
cout << "It worked!"<<endl;
}
else
{
cout << "You messed up"<<endl;
}
}
It gives me this error: 23:22: error: no matching function for call to ‘std::basic_ifstream::open(std::string&)’
23:22: note: candidate is: /usr/include/c++/4.6/fstream:531:7: note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char, _Traits = std::char_traits, std::ios_base::openmode = std::_Ios_Openmode] /usr/include/c++/4.6/fstream:531:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string}’ to ‘const char*’
So, I'm not really sure what the problem is. I'm fairly new to programming, I'm trying to figure this out for a class project(this isn't the assignment, it's just a generic version of the problem I'm having in my project so far). If you can give any help, that would be great. Thanks!
Aucun commentaire:
Enregistrer un commentaire