gpt4 book ai didi

c++ - 将字符串放入 ifstream 方法中

转载 作者:太空狗 更新时间:2023-10-29 19:45:07 24 4
gpt4 key购买 nike

<分区>

我正在学习 C++,当我尝试在 ifstream 方法中使用 String 时遇到了一些麻烦,如下所示:

string filename;
cout << "Enter the name of the file: ";
cin >> filename;
ifstream file ( filename );

完整代码如下:

// obtaining file size
#include <iostream>
#include <fstream>
using namespace std;

int main ( int argc, char** argv )
{
string file;
long begin,end;
cout << "Enter the name of the file: ";
cin >> file;
ifstream myfile ( file );
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.close();
cout << "File size is: " << (end-begin) << " Bytes.\n";

return 0;
}

这里是Eclipse的报错,方法前的x:

no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)'

但是当我尝试在 Eclipse 中编译时,它在方法之前放置了一个 x,这表明语法中有错误,但是语法中有什么错误呢?谢谢!

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com