How do i get the contents of a file into a buffer so that i can use some <string> functions on it?
Cheers
Printable View
How do i get the contents of a file into a buffer so that i can use some <string> functions on it?
Cheers
Dunno if this is what you wanted:
http://www.arachnoid.com/cpptutor/student2.html
Sorry i wasn't to clear, i know how to ge a file into a buffer but i want to convert it from (const * char) to (string) but i don't know how to.Quote:
Originally Posted by g4j31a5
I hope that makes sence.
I think the fstream's getline() function can have a string as the output parameter (CMIIW). Or why don't you use the copy constructor of the string? E.g.
Code:char str1[]="Hello World!";
std::string str2=str1;
If you want to read the entire contents of a file into a string object you can use a stringstream as an intermediate object.
Code:ifstream file("foo.txt");
stringstream sstr;
string info;
if( file.is_open() )
{
sstr << file.rdbuf(); // Read entire file into stringstream
info = sstr.str(); // Convert stringstream into string
// Do whatever you want with the string "info".
}
Thanks mate that is exactly what i needed, cheers.Quote:
Originally Posted by hk_mp5kpdw