ifstream文件目录带引号+组合目录c ++

时间:2017-04-11 05:27:01

标签: c++ ifstream

这条线正在运作

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方法后,它无法读取文本文件的内容,BOTH编译没有错误

1 个答案:

答案 0 :(得分:1)

查看ifstream文档 - ifstream不提供接受keyboard-keys-from-name.h的构造函数,只有一个接受std::string

所以试试这个:

char const*

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

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

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