error when initialize ifstream with string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lingyun Yang

    error when initialize ifstream with string

    hi every one,

    I meet a compile error in my small homework program:
    Can I innitialize ifstream with a string?
    or I must come back to char* style string?


    Thank you!

    Lingyun


    //----------- here is part of my small program ---------------
    string dicname("tofel. bok.gb2312");

    char buf[256];
    ifstream fdic(dicname);
    while(fdic.getl ine(buf,256))
    {}

    //---------------- Here is the compile error ------------------
    /home/lyyang/cpp-proj/wordwar.cpp: In function `int main()':
    /home/lyyang/cpp-proj/wordwar.cpp:29: error: no matching function for
    call to `
    std::basic_ifst ream<char, std::char_trait s<char> >::basic_ifstre am(
    std::string&)'
    /usr/lib/gcc-lib/i386-pc-linux-gnu/3.3.2/include/g++-v3/iosfwd:89:
    error: candidates
    are: std::basic_ifst ream<char, std::char_trait s<char>[color=blue]
    >::basic_ifstre am(const std::basic_ifst ream<char,[/color]
    std::char_trait s<char> >&)
    /usr/lib/gcc-lib/i386-pc-linux-gnu/3.3.2/include/g++-v3/fstream:519:
    error:
    std::basic_ifst ream<_CharT, _Traits>::basic _ifstream(const
    char*, std::_Ios_Openm ode) [with _CharT = char, _Traits =
    std::char_trait s<char>]
    /usr/lib/gcc-lib/i386-pc-linux-gnu/3.3.2/include/g++-v3/fstream:504:
    error:
    std::basic_ifst ream<_CharT, _Traits>::basic _ifstream()
    [with
    _CharT = char, _Traits = std::char_trait s<char>]
  • Leor Zolman

    #2
    Re: error when initialize ifstream with string

    On Sun, 11 Apr 2004 01:36:16 GMT, Lingyun Yang <yzine@insightb b.com> wrote:
    [color=blue]
    >hi every one,
    >
    >I meet a compile error in my small homework program:
    >Can I innitialize ifstream with a string?
    >or I must come back to char* style string?[/color]

    The fstream constructors do require a char* style string, but fortunately
    it is trivial to get one from a std::string: just apply the c_str() member
    function, e.g.:
    ifstream fdic(dicname.c_ str());
    -leor

    [color=blue]
    >
    >
    >Thank you!
    >
    >Lingyun
    >
    >
    >//----------- here is part of my small program ---------------
    >string dicname("tofel. bok.gb2312");
    >
    >char buf[256];
    >ifstream fdic(dicname);
    >while(fdic.get line(buf,256))
    >{}[/color]

    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: Download BD Software's free STL Error Message Decryptor at:
    An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

    Comment

    • Leor Zolman

      #3
      Re: error when initialize ifstream with string

      On Sun, 11 Apr 2004 01:36:16 GMT, Lingyun Yang <yzine@insightb b.com> wrote:
      [color=blue]
      >hi every one,
      >
      >I meet a compile error in my small homework program:
      >Can I innitialize ifstream with a string?
      >or I must come back to char* style string?[/color]

      The fstream constructors do require a char* style string, but fortunately
      it is trivial to get one from a std::string: just apply the c_str() member
      function, e.g.:
      ifstream fdic(dicname.c_ str());
      -leor

      [color=blue]
      >
      >
      >Thank you!
      >
      >Lingyun
      >
      >
      >//----------- here is part of my small program ---------------
      >string dicname("tofel. bok.gb2312");
      >
      >char buf[256];
      >ifstream fdic(dicname);
      >while(fdic.get line(buf,256))
      >{}[/color]

      --
      Leor Zolman --- BD Software --- www.bdsoft.com
      On-Site Training in C/C++, Java, Perl and Unix
      C++ users: Download BD Software's free STL Error Message Decryptor at:
      An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

      Comment

      • David Harmon

        #4
        Re: error when initialize ifstream with string

        On Sun, 11 Apr 2004 01:36:16 GMT in comp.lang.c++, Lingyun Yang
        <yzine@insightb b.com> wrote,[color=blue]
        >ifstream fdic(dicname);[/color]

        ifstream fdic(dicname.c_ str());


        Comment

        • David Harmon

          #5
          Re: error when initialize ifstream with string

          On Sun, 11 Apr 2004 01:36:16 GMT in comp.lang.c++, Lingyun Yang
          <yzine@insightb b.com> wrote,[color=blue]
          >ifstream fdic(dicname);[/color]

          ifstream fdic(dicname.c_ str());


          Comment

          Working...