Trying to use a string variable with infile.open() is treated as a char in c++

纵饮孤独 提交于 2019-12-02 11:36:04

问题


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!


回答1:


There are two overloads for open:

void open( const char *filename, ios_base::openmode mode = ios_base::in );
void open( const std::string &filename, ios_base::openmode mode = ios_base::in );

The second one is only available since C++11. You are apparently either not compiling in C++11 mode or using an out of date compiler. There is another overload that takes const char*, so this should work regardless:

infile.open(fileName.c_str());



回答2:


you can do following way

ifstream aStream;    
 aStream.open(textFile.c_str());



回答3:


Replace infile.open(fileName); with infile.open(fileName.c_str());

open() takes a parameter of type const char*. In your case, you are trying to pass a string. The above code should work for your issue.




回答4:


The answers already given are correct, but the entire answer is scattered between 2 answers and a comment. Here's a more complete answer.

The open() you have used is the overloaded version available since C++11, which takes an std::string parameter for the filename.

void open( const std::string &filename, ios_base::openmode mode = ios_base::in );

However, your compiler seems to have the default behavior of compiling in a pre-C++11 standard, under which open() can only take a const char* parameter (ie, a C string).

void open( const char *filename, ios_base::openmode mode = ios_base::in );

To fix your problem, you can do one of two things:

  • If your compiler supports c++11 compilation, google how to do that. In g++, for example, you'd use g++ -std=c++11 myprogram.cpp (or better yet, the latest C++14 standard with g++ -std=c++14 myprogram.cpp)
  • If you don't want to or can't compile that way, then change your open() to use a C string - infile.open(fileName.c_str());


来源:https://stackoverflow.com/questions/29725759/trying-to-use-a-string-variable-with-infile-open-is-treated-as-a-char-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!