gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:11:54 24 4
gpt4 key购买 nike

这条线路正在运行

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编译没有错误

最佳答案

看看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\world vs . 测试“hello world”)。但是在 C++ 代码中,您已经了一个字符串(包含空格,如果有的话)。然后附加引号被解释为文件路径的一部分,这当然会导致无效引号(除非您的路径中有一个目录和一个名为 "的文件 - 这实际上在 linux(!)下是可能的:`/工作/目录/"/home/pi/[...]/file/").

关于c++ - ifstream文件目录带引号+组合目录c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43337384/

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