C++ ifstream error using string as opening file path.











up vote
62
down vote

favorite
17












I have:



string filename: 
ifstream file(filename);


The compilers complains about no match between ifstream file and a string. Do I need to convert filename to something?



Here's the error:



error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)’
/usr/include/c++/4.4/fstream:454: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]









share|improve this question




















  • 1




    I'm sure you could improve the title of this question.
    – Lightness Races in Orbit
    Jun 12 '11 at 18:12










  • I changed it to ifstream error.
    – Mark
    Jun 12 '11 at 18:13










  • That's still incredibly vague. Can't you make it so that it actually describes the specific issue?
    – Lightness Races in Orbit
    Jun 12 '11 at 18:34















up vote
62
down vote

favorite
17












I have:



string filename: 
ifstream file(filename);


The compilers complains about no match between ifstream file and a string. Do I need to convert filename to something?



Here's the error:



error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)’
/usr/include/c++/4.4/fstream:454: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]









share|improve this question




















  • 1




    I'm sure you could improve the title of this question.
    – Lightness Races in Orbit
    Jun 12 '11 at 18:12










  • I changed it to ifstream error.
    – Mark
    Jun 12 '11 at 18:13










  • That's still incredibly vague. Can't you make it so that it actually describes the specific issue?
    – Lightness Races in Orbit
    Jun 12 '11 at 18:34













up vote
62
down vote

favorite
17









up vote
62
down vote

favorite
17






17





I have:



string filename: 
ifstream file(filename);


The compilers complains about no match between ifstream file and a string. Do I need to convert filename to something?



Here's the error:



error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)’
/usr/include/c++/4.4/fstream:454: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]









share|improve this question















I have:



string filename: 
ifstream file(filename);


The compilers complains about no match between ifstream file and a string. Do I need to convert filename to something?



Here's the error:



error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)’
/usr/include/c++/4.4/fstream:454: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]






c++ ifstream






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 3 '12 at 20:48









KronoS

4,10784676




4,10784676










asked Jun 12 '11 at 18:08









Mark

3,112114166




3,112114166








  • 1




    I'm sure you could improve the title of this question.
    – Lightness Races in Orbit
    Jun 12 '11 at 18:12










  • I changed it to ifstream error.
    – Mark
    Jun 12 '11 at 18:13










  • That's still incredibly vague. Can't you make it so that it actually describes the specific issue?
    – Lightness Races in Orbit
    Jun 12 '11 at 18:34














  • 1




    I'm sure you could improve the title of this question.
    – Lightness Races in Orbit
    Jun 12 '11 at 18:12










  • I changed it to ifstream error.
    – Mark
    Jun 12 '11 at 18:13










  • That's still incredibly vague. Can't you make it so that it actually describes the specific issue?
    – Lightness Races in Orbit
    Jun 12 '11 at 18:34








1




1




I'm sure you could improve the title of this question.
– Lightness Races in Orbit
Jun 12 '11 at 18:12




I'm sure you could improve the title of this question.
– Lightness Races in Orbit
Jun 12 '11 at 18:12












I changed it to ifstream error.
– Mark
Jun 12 '11 at 18:13




I changed it to ifstream error.
– Mark
Jun 12 '11 at 18:13












That's still incredibly vague. Can't you make it so that it actually describes the specific issue?
– Lightness Races in Orbit
Jun 12 '11 at 18:34




That's still incredibly vague. Can't you make it so that it actually describes the specific issue?
– Lightness Races in Orbit
Jun 12 '11 at 18:34












3 Answers
3






active

oldest

votes

















up vote
126
down vote



accepted










Change



ifstream file(filename);


to



ifstream file(filename.c_str());


Because the constructor for an ifstream takes a const char*, not a string pre-C++11.






share|improve this answer






























    up vote
    11
    down vote













    The ifstream constructor expects a const char*, so you need to do ifstream file(filename.c_str()); to make it work.






    share|improve this answer




























      up vote
      2
      down vote













      in c++-11 it can also be an std::string. So (installing c++-11 and) changing the dialect of you project to c++-11 could also fix the problem.






      share|improve this answer





















      • while your answer is correct, it is already present in the accepted answer, although not so clearly: not a string pre-C++11.
        – Chris Maes
        Jan 6 '16 at 8:06











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6323619%2fc-ifstream-error-using-string-as-opening-file-path%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      126
      down vote



      accepted










      Change



      ifstream file(filename);


      to



      ifstream file(filename.c_str());


      Because the constructor for an ifstream takes a const char*, not a string pre-C++11.






      share|improve this answer



























        up vote
        126
        down vote



        accepted










        Change



        ifstream file(filename);


        to



        ifstream file(filename.c_str());


        Because the constructor for an ifstream takes a const char*, not a string pre-C++11.






        share|improve this answer

























          up vote
          126
          down vote



          accepted







          up vote
          126
          down vote



          accepted






          Change



          ifstream file(filename);


          to



          ifstream file(filename.c_str());


          Because the constructor for an ifstream takes a const char*, not a string pre-C++11.






          share|improve this answer














          Change



          ifstream file(filename);


          to



          ifstream file(filename.c_str());


          Because the constructor for an ifstream takes a const char*, not a string pre-C++11.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 3 '12 at 23:18

























          answered Jun 12 '11 at 18:09









          Seth Carnegie

          60.2k14146213




          60.2k14146213
























              up vote
              11
              down vote













              The ifstream constructor expects a const char*, so you need to do ifstream file(filename.c_str()); to make it work.






              share|improve this answer

























                up vote
                11
                down vote













                The ifstream constructor expects a const char*, so you need to do ifstream file(filename.c_str()); to make it work.






                share|improve this answer























                  up vote
                  11
                  down vote










                  up vote
                  11
                  down vote









                  The ifstream constructor expects a const char*, so you need to do ifstream file(filename.c_str()); to make it work.






                  share|improve this answer












                  The ifstream constructor expects a const char*, so you need to do ifstream file(filename.c_str()); to make it work.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 12 '11 at 18:09









                  Alexander Gessler

                  38.6k569113




                  38.6k569113






















                      up vote
                      2
                      down vote













                      in c++-11 it can also be an std::string. So (installing c++-11 and) changing the dialect of you project to c++-11 could also fix the problem.






                      share|improve this answer





















                      • while your answer is correct, it is already present in the accepted answer, although not so clearly: not a string pre-C++11.
                        – Chris Maes
                        Jan 6 '16 at 8:06















                      up vote
                      2
                      down vote













                      in c++-11 it can also be an std::string. So (installing c++-11 and) changing the dialect of you project to c++-11 could also fix the problem.






                      share|improve this answer





















                      • while your answer is correct, it is already present in the accepted answer, although not so clearly: not a string pre-C++11.
                        – Chris Maes
                        Jan 6 '16 at 8:06













                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote









                      in c++-11 it can also be an std::string. So (installing c++-11 and) changing the dialect of you project to c++-11 could also fix the problem.






                      share|improve this answer












                      in c++-11 it can also be an std::string. So (installing c++-11 and) changing the dialect of you project to c++-11 could also fix the problem.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 5 '16 at 20:17









                      evi v

                      215




                      215












                      • while your answer is correct, it is already present in the accepted answer, although not so clearly: not a string pre-C++11.
                        – Chris Maes
                        Jan 6 '16 at 8:06


















                      • while your answer is correct, it is already present in the accepted answer, although not so clearly: not a string pre-C++11.
                        – Chris Maes
                        Jan 6 '16 at 8:06
















                      while your answer is correct, it is already present in the accepted answer, although not so clearly: not a string pre-C++11.
                      – Chris Maes
                      Jan 6 '16 at 8:06




                      while your answer is correct, it is already present in the accepted answer, although not so clearly: not a string pre-C++11.
                      – Chris Maes
                      Jan 6 '16 at 8:06


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6323619%2fc-ifstream-error-using-string-as-opening-file-path%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      RAC Tourist Trophy