error when initialize ifstream with string

L

Lingyun Yang

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 said:
::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>]
 
L

Leor Zolman

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))
{}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top