【问题标题】:Ifstream file directory with quotation+combination directory c++ifstream文件目录带引号+组合目录c++
【发布时间】: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 的定义都缺失了。)

标签: c++ ifstream


【解决方案1】:

查看ifstream 文档 - ifstream 不提供接受std::string 的构造函数,只有一个接受char const*

所以试试这个:

std::ifstream file((path+dicfile).c_str());

编辑: www.cplusplus.com 已过期!从 C++11 开始,ifstream 确实支持构造函数 accepting std::string

显然,您的编译器没有启用 C++11。 GCC 和 CLANG 都接受 -std=c++11 (或 -std=c++1y 用于更新(至少 GCC 5.4))。 MSVC 见here

Edit2:正如您自己发现的那样,引号不是必需的(从我的答案中删除它们 - 虽然应该注意到我自己......)。但为什么?答案很简单:在命令行上,即使有空格,您也必须将它们放置以获取单个字符串(或者,至少在 linux 下,您可以转义空格:test hello\ worldtest "hello world")。但是在 C++ 代码中,您已经有一个字符串(如果有的话,包含空格)。额外的引号然后被解释为文件路径的一部分,这当然会导致无效的(除非你的路径中有一个目录和一个文件都名为 " - 这实际上在 linux(!) 下是可能的:`/工作/目录/"/home/pi/[...]/file/")。

【讨论】:

  • 哇!太好了,它工作!我能知道 .c_str() 是做什么的吗?
  • @JackdonChew 它将内部字符串表示形式返回为经典的 C 字符串:c_str
  • 其实是it does,从C++11开始。
  • 它可以编译没有错误:(但它不会工作到目录
  • @JackdonChew 如果您无法打开文件,构造函数不会因异常而失败。你必须检查file.fail(),如果需要检查errno,就像我之前链接的answer一样......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-11
相关资源
最近更新 更多