This line is working
ifstream file("/home/pi/Desktop/DMixer_Webinterface_Downloadfile/Testing_Read.txt");
I split the path to 2 string
string path = "/home/pi/Desktop/DMixer_Webinterface_Downloadfile/";
string dicfile = "Testing_Read.txt";
Combine them
ifstream file("\""+path+dicfile+"\"");
It have error that
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>&’
Updated Code * Previous compilation error solved
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++;
}
By using this line I can print out the content of the text file
ifstreamfile("/home/pi/Desktop/DMixer_Webinterface_Downloadfile/Testing_Read.txt");
But, after using combination the directory path and put to the ifstream method, it cannot read the content of the text file, BOTH compilation no error
Have a look at ifstream documentation - ifstream does not offer a constructor accepting std::string
, only one accepting char const*
.
So try this instead:
std::ifstream file((path+dicfile).c_str());
Edit: www.cplusplus.com is out of date! Since C++11, ifstream does support a constructor accepting std::string.
Obviously, your compiler does not have C++11 enabled. GCC and CLANG both accept -std=c++11 (or -std=c++1y for newer (GCC 5.4 at least)). MSVC see here.
Edit2: As you found out yourself, the quotation marks are not necessary (removed them from my answer - should have noticed myself, though...). But why? Answer is easy: On the command line, you have to place them to get one single string even if there are spaces (or, at least under linux, you can escape the spaces: test hello\ world
vs. test "hello world"
). But in the C++ code, you have already a single string (containing the spaces, if there are). Additional quotes then are interpreted as part of the file path, which, of course, results in an invalid one (unless you have a directory and a file both named " in your path - which actually is possible under linux(!): `/working/directory/"/home/pi/[...]/file/").