Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 发现此文件读取代码中的错误(C+;+;)_C++_File Io - Fatal编程技术网

C++ 发现此文件读取代码中的错误(C+;+;)

C++ 发现此文件读取代码中的错误(C+;+;),c++,file-io,C++,File Io,有人能告诉我为什么这个方法不能编译吗 void Statistics::readFromFile(string filename) { string line; ifstream myfile (filename); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); cout <<

有人能告诉我为什么这个方法不能编译吗

void Statistics::readFromFile(string filename)
{
    string line;
    ifstream myfile (filename);
    if (myfile.is_open())
    {
        while (! myfile.eof() )
        {
            getline (myfile,line);
            cout << line << endl;
        }
        myfile.close();
    }

    else cout << "Unable to open file"; 

}
void Statistics::readFromFile(字符串文件名)
{
弦线;
ifstream myfile(文件名);
如果(myfile.is_open())
{
而(!myfile.eof())
{
getline(myfile,line);
库特
应该是:

ifstream myfile (filename.c_str() );
while ( getline( myfile,line ) ){
   cout << line << endl;
}
此外,您的读取循环逻辑错误。它应该是:

ifstream myfile (filename.c_str() );
while ( getline( myfile,line ) ){
   cout << line << endl;
}
while(getline(myfile,line)){
cout构造函数具有以下签名

explicit ifstream ( const char * filename, ios_base::openmode mode = ios_base::in );
您需要传入一个常量char*和一个模式,例如:

ifstream ifs ( "test.txt" , ifstream::in );
该模式是可选的,因为它定义了一个默认值,所以您可以使用:

ifstream myfile ( filename.c_str() );

您应该使用fileName.c_str()以便将const char*指针传递给myFile结构。

读取编译器错误:

no matching function for call to 'std::basic_ifstream >::basic_ifstream(std::string*)
没有用于调用的匹配函数:
它找不到您尝试调用的函数

std::basic\u ifstream>:
-ifstream的成员函数

:基本\u ifstream(std::string*)
-以字符串指针作为参数的构造函数

因此,您尝试通过向其构造函数传递字符串指针来创建ifstream,但它找不到接受此类参数的构造函数

由于您没有在上面传递字符串指针,因此您发布的代码必须与实际代码不同。在询问代码时,请始终复制/粘贴。拼写错误使您无法找出问题所在。在任何情况下,我记得,构造函数都不接受字符串参数,而只接受常量char*.So filename.c_str()我们应该这样做

除此之外,您可以做得更简单:

ifstream myfile (filename);
    std::copy(std::istream_itrator<std::string>(myfile),
              std::istream_itrator<std::string>(),
              std::ostream_iterator<std::string>(std::cout));
}
ifstream-myfile(文件名);
std::copy(std::istream\u itrator(myfile),
std::istream_itrator(),
std::ostream_迭代器(std::cout));
}

C++11标准已经解决了这个缺陷。
std::ifstream myfile(filename);
现在应该可以编译了,当
filename
的类型为
std::string

aaaaaaah好的!非常感谢!@Bill似乎你没有。@Neil但不会eof()第一次简单地返回false?因为它是有效的。@bill nope-如果流是为读取而打开的,“i”代表input@Neil:是的,我的无知。在我链接到它的文档中,甚至显示有一个默认值ios_base::In.Duh.:)
ifstream myfile (filename);
    std::copy(std::istream_itrator<std::string>(myfile),
              std::istream_itrator<std::string>(),
              std::ostream_iterator<std::string>(std::cout));
}