- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这条线路正在运行
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/
我有一个类似 ( www.bbcurdu.com) 的网站。现在我的问题是,当我添加一个带有引号或引号的新故事时,它不会保存在数据库中。如果我删除这些逗号或引号,那么它将保存在数据库中。并正确获取和显
我正在使用(并坚持使用)以下版本的 Ruby: ruby 1.8.7 (2012-06-29 patchlevel 370) [x86_64-linux] 我尝试了很多谷歌搜索,但找不到解决我的问题的
我有以下示例数据, 我想连接 string1 和 string2 以产生连接结果 "a""b" 使用连接函数 我在官方的microsoft excel文档中没有找到任何答案 https://suppo
我是 SQL-mySQL 方面的 super 新手,我想查询这些包含具有特定值的列的条目,但该值中已经有引号,它实际上看起来像这样,引号已经存在:“荷兰鹿特丹 Voor Land-en Volkenk
是否有一个简单的正则表达式来匹配所有 unicode 引号?还是必须像这样手动编码: quotes = ur"[\"'\u2018\u2019\u201c\u201d]" 感谢阅读。 布莱恩 最佳答案
我有下面的代码,禁止将特殊字符插入到 HTML 表单上提交的文本中。 可以看出,我需要禁用的特殊字符位于引号之间,而我需要禁用引号 " 本身,但我不能这样做,因为如果我添加另一个中间的引号然后读起来就
对于给定的输入字符串 abc,[def,ghi,ijk],lm,(no,pq,rs),[tu,vw,xy],zs,"as,as,fr"输出应该是 abc [def,ghi,ijk] lm (no,pq
下午好,快乐 4 我在处理一些 javascript 时遇到了问题....我是一个初学者,正在使用这个特定的在线示例来了解如何淡入和淡出多个引号... 我已经实现了 HTML/Javascript/C
我想在一段文本周围加上引号。文本不能超过 3 行:如果超过,我想要一个省略号(并且我仍然想要引号)。我附上我现在拥有的东西。我的解决方案使用 JS:虽然纯 CSS 解决方案是理想的,但可能的 CSS
我将它发送给一个函数,我想在下面的变量值周围加上双引号,例如 $var = "New York"(注意引号) $fq.=" + area:$state"; 所以当我回显 $state 时我想用双
这个问题在这里已经有了答案: How can I make Java print quotes, like "Hello"? (11 个答案) 关闭 9 年前。 我想在 java 中打印反引号。但是
我认为这是 F# 的一个众所周知的限制,但我找不到任何好的解决方法…… 所以,这里是代码(我试图让它尽可能简单,所以它可能看起来没有任何意义): [] type Human (makeAName: u
我在Windows 7上,在Apache 2.4上使用PHP 5.6.14版:我必须使用PHP在SQLite3数据库上构建查询选择。 NOTA:我是PHP的再见..... 我的代码如下 '; echo
在 jQuery 文档中,大多数字符串都是使用单引号“声明”的。包括documentation用于 jQuery UI。 那为什么不呢 $(".datepicker").datepicker({ da
我正在尝试在 Java 中使用以下正则表达式,它应该与任何 lang="2-char-lang-name" 匹配: String lang = "lang=\"" + L.detectLang(inp
我试图在 more 之后插入一个 block 引用指令,不接受任何参数: First paragraph. .. more:: Blockquote here! -- Author S
我从外部进程获得了一些我无法更改的 JSON,并且我需要修改此 JSON 字符串才能使下游 Java 进程正常工作。 JSON 字符串如下所示: {"widgets":"blah","is_dog":
我试图在从数据库读取数据后生成 CSV 文件。现在单个数据可以包含逗号、单引号和双引号。 请告诉我如何在正则表达式的帮助下处理这种情况。 最佳答案 您可以为 CSV 文件使用不同的分隔符吗?也许是一个
我尝试在 codemodel(Sun) 中创建下一个表达式: driver.findElement(By.xpath("//div[text()=\""+whatToclick+"\"]/pare
我需要知道报价和列表之间的区别。例如: cl-prompt> (equal (first (list * 1 2)) *) T cl-prompt> (equal (first '(* 1 2)) *
我是一名优秀的程序员,十分优秀!