【发布时间】:2017-09-06 08:04:26
【问题描述】:
这条线正在工作
ifstream file("/home/pi/Desktop/DMixer_Webinterface_Downloadfile/Testing_Read.txt");
我将路径拆分为 2 个字符串
string path = "/home/pi/Desktop/DMixer_Webinterface_Downloadfile/";
string dicfile = "Testing_Read.txt";
将它们组合起来
ifstream file("\""+path+dicfile+"\"");
它有错误
testing030320170800.cpp: In function ‘int main()’:
testing030320170800.cpp:3221:38: error: no matching function for call to ‘std::basic_ifstream<char>::basic_ifstream(std::basic_string<char>)’
ifstream file("\""+path+dicfile+"\"");
^
testing030320170800.cpp:3221:38: note: candidates are:
In file included from testing030320170800.cpp:17:0:
/usr/include/c++/4.9/fstream:470:7: note: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
^
/usr/include/c++/4.9/fstream:470:7: note: no known conversion for argument 1 from ‘std::basic_string<char>’ to ‘const char*’
/usr/include/c++/4.9/fstream:456:7: note: std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char; _Traits = std::char_traits<char>]
basic_ifstream() : __istream_type(), _M_filebuf()
^
/usr/include/c++/4.9/fstream:456:7: note: candidate expects 0 arguments, 1 provided
/usr/include/c++/4.9/fstream:430:11: note: std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)
class basic_ifstream : public basic_istream<_CharT, _Traits>
^
/usr/include/c++/4.9/fstream:430:11: note: no known conversion for argument 1 from ‘std::basic_string<char>’ to ‘const std::basic_ifstream<char>&’
更新的代码 * 以前的编译错误已解决
string path = "/home/pi/Desktop/DMixer_Webinterface_Downloadfile/";
string dicfile = "Testing_Read.txt";
cout << (("\""+path+dicfile+"\""));
//ifstream file("/home/pi/Desktop/DMixer_Webinterface_Downloadfile/Testing_Read.txt");
ifstream file(("\""+path+dicfile+"\"").c_str());
std::string str;
while (std::getline(file, str,','))
{
myArray[array_count] = str; // store those value in array
cout << str << "\n";
strings.push_back(str);
array_count++;
}
通过使用这一行,我可以打印出文本文件的内容
ifstreamfile("/home/pi/Desktop/DMixer_Webinterface_Downloadfile/Testing_Read.txt");
但是,使用组合目录路径并放入ifstream方法后,无法读取文本文件的内容,编译都没有错误
【问题讨论】:
-
错误信息说得很清楚:
ifstream的构造函数的参数必须是您系统上的const char*。 -
您只能使用 C++11 编译器(或更高版本)使用
std::string构造std::ifstream。在命令行中使用-std=c++11。 -
在我尝试将 const char 返回到路径后,它可以编译而没有错误。但是,为什么看起来 ifstream 文件不起作用?那里没有输出
-
@JackdonChew 读取文件的代码是什么样的?您应该在此处发布,以便我们为您提供帮助。
-
@JackdonChew 现在你已经更新了你的代码:请看看如何写一个minimal, complete, verifiable example。 (为了完整起见,array_count、myArray 和 strings 的定义都缺失了。)