
我正在学习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,表示语法中有错误,但是语法有什么问题?谢谢!
解决方法 你应该将char *传递给ifstream构造函数,使用c_str()函数.// includes !!!#include <fstream>#include <iostream>#include <string>using namespace std;int main() { string filename; cout << "Enter the name of the file: "; cin >> filename; ifstream file ( filename.c_str() ); // c_str !!!} 总结 以上是内存溢出为你收集整理的c – 在ifstream方法中放置一个字符串全部内容,希望文章能够帮你解决c – 在ifstream方法中放置一个字符串所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)