0

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

Jackdon Chew
  • 117
  • 1
  • 2
  • 10
  • 1
    The error message says it quite clearly: the argument to `ifstream`'s constructor must be a `const char*` on your system. – ForceBru Apr 11 '17 at 05:32
  • 2
    You can construct a `std::ifstream` using a `std::string` only with a C++11 compiler (or later). Use `-std=c++11` in the command line. – R Sahu Apr 11 '17 at 05:34
  • After I try return a const char to the path, it can compile with no error. But, why looks like ifstream file didnt work ? No output there – Jackdon Chew Apr 11 '17 at 05:58
  • @JackdonChew How does your code for reading the file look like? You should post it here so that we can help you out. – Aconcagua Apr 11 '17 at 06:03
  • @JackdonChew Now that you have updated your code: Please have a look at how to write a [minimal, complete, verifiable example](https://stackoverflow.com/help/mcve). (For completeness, the definitions for array_count, myArray and strings are missing.) – Aconcagua Apr 11 '17 at 06:37

1 Answers1

1

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/").

Community
  • 1
  • 1
Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • wow ! excellent, it work ! Can i know that what does .c_str() do ? – Jackdon Chew Apr 11 '17 at 05:39
  • @JackdonChew It returns the internal string representation as a classic C string: [c_str](http://www.cplusplus.com/reference/string/string/c_str/) – Aconcagua Apr 11 '17 at 05:41
  • Actually [it does](http://en.cppreference.com/w/cpp/io/basic_ifstream/basic_ifstream) since C++11. – juanchopanza Apr 11 '17 at 05:43
  • It can compile with no error :( But it wont work to the directory – Jackdon Chew Apr 11 '17 at 05:50
  • @JackdonChew "won't work" is quite short hand... Is the file path really correct (you seem to be under linux, so consider case as well!)? Do you have read access on your file (try ls -l in the directory, if need be: chmod 0644 )? Try to get some hints from errno, too, as described [here](http://stackoverflow.com/a/17338934/1312382). – Aconcagua Apr 11 '17 at 06:01
  • Yup, I am working under linux , the permission all I had open access . Since I can work for ifstream file("/home/pi/Desktop/DMixer_Webinterface_Downloadfile/Testing_Read.txt");, it should be not access problem – Jackdon Chew Apr 11 '17 at 06:08
  • 1
    @JackdonChew The constructor won't fail with exception if you cannot open the file. You have to check with `file.fail()` and if need be examine `errno` as in the [answer](http://stackoverflow.com/questions/17337602/how-to-get-error-message-when-ifstream-open-fails/17338934#17338934) I linked to before already... – Aconcagua Apr 11 '17 at 06:14
  • @JackdonChew Should have worked then, yes. I assume you have an `std::vector strings;` somewhere. How is your array defined (by the way: why do you need to store the lines twice???). Is `array_count` correctly initialised to 0? – Aconcagua Apr 11 '17 at 06:32
  • I am sorry about that, now I echo back it have status No such file or directory – Jackdon Chew Apr 11 '17 at 06:33
  • How to edit your answer ? I found out the root cause is the ""\", after using .c_str(), no longer needed the quotation bcoz it already const char, just update your answer with std::ifstream file((path+dicfile).c_str()); – Jackdon Chew Apr 11 '17 at 06:38
  • @JackdonChew Ah, should have thought about that myself - edited the anser and added an explanation... – Aconcagua Apr 11 '17 at 06:48