error when initialize ifstream with string

Discussion in 'C++' started by Lingyun Yang, Apr 11, 2004.

  1. Lingyun Yang

    Lingyun Yang Guest

    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.getline(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_ifstream<char, std::char_traits<char> >::basic_ifstream(
    std::string&)'
    /usr/lib/gcc-lib/i386-pc-linux-gnu/3.3.2/include/g++-v3/iosfwd:89:
    error: candidates
    are: std::basic_ifstream<char, std::char_traits<char>
    >::basic_ifstream(const std::basic_ifstream<char,

    std::char_traits<char> >&)
    /usr/lib/gcc-lib/i386-pc-linux-gnu/3.3.2/include/g++-v3/fstream:519:
    error:
    std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const
    char*, std::_Ios_Openmode) [with _CharT = char, _Traits =
    std::char_traits<char>]
    /usr/lib/gcc-lib/i386-pc-linux-gnu/3.3.2/include/g++-v3/fstream:504:
    error:
    std::basic_ifstream<_CharT, _Traits>::basic_ifstream()
    [with
    _CharT = char, _Traits = std::char_traits<char>]
    Lingyun Yang, Apr 11, 2004
    #1
    1. Advertising

  2. Lingyun Yang

    Leor Zolman Guest

    On Sun, 11 Apr 2004 01:36:16 GMT, Lingyun Yang <> wrote:

    >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?


    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


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


    --
    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:
    www.bdsoft.com/tools/stlfilt.html
    Leor Zolman, Apr 11, 2004
    #2
    1. Advertising

  3. Lingyun Yang

    David Harmon Guest

    On Sun, 11 Apr 2004 01:36:16 GMT in comp.lang.c++, Lingyun Yang
    <> wrote,
    >ifstream fdic(dicname);


    ifstream fdic(dicname.c_str());
    David Harmon, Apr 11, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Herv? LEBAIL
    Replies:
    6
    Views:
    529
    DrUg13
    Feb 9, 2004
  2. Leo

    ifstream & string

    Leo, Mar 7, 2007, in forum: C++
    Replies:
    3
    Views:
    482
    Mike Wahler
    Mar 10, 2007
  3. Assertor
    Replies:
    5
    Views:
    379
  4. Chris Forone

    ifstream/string ctor

    Chris Forone, Dec 16, 2007, in forum: C++
    Replies:
    4
    Views:
    404
    Chris Forone
    Dec 18, 2007
  5. Wolfnoliir

    ifstream >> string with UTF-8?

    Wolfnoliir, Sep 9, 2009, in forum: C++
    Replies:
    6
    Views:
    1,195
    Wolfnoliir
    Sep 9, 2009
Loading...

Share This Page