file io - C++ compiler error with stringbuf / ifstream -
i cannot understand why compiler (msvc++2010) doesn't code:
// get_sum(filename c-string) returns sum file int get_sum(const char* const s) { stringbuf bill_buf; ifstream bill_file; bill_file.open(s); bill_file.get(bill_buf, '\0'); // read whole file bill_file.close(); return get_sum_from_string(bill_buf.str()); }
i these errors (i translated them german english , give correct line numbers code excerpt without leading comment):
error 1 error c2079: 'bill_buf' uses undefined class 'std::basic_stringbuf<_elem,_traits,_alloc>' (line 2)
error 2 error c2664: 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::get(_elem *,std::streamsize)': conversion of parameter 1 'int' 'char *' not possible (line 5)
error 3 error c2228: left of ".str" there must class/structure/union. (line 7)
has got idea what's going on there? lot! (if has got better idea how whole file contents string, i'd appreciate it)
you're missing include. here's code, time without using streambuf
:
#include<fstream> #include<string> #include<iterator> int get_sum(const char* const s) { std::ifstream bill_file(s); std::string contents((std::istreambuf_iterator<char>(bill_file)), std::istreambuf_iterator<char>()); return get_sum_from_string(contents); }
Comments
Post a Comment