How do i get the contents of a file into a buffer so that i can use some <string> functions on it?
Cheers
This is a discussion on string manipulation within the C++ Programming forums, part of the General Programming Boards category; How do i get the contents of a file into a buffer so that i can use some <string> functions ...
How do i get the contents of a file into a buffer so that i can use some <string> functions on it?
Cheers
Last edited by wart101; 12-11-2006 at 12:54 AM.
WhAtHA hell Is GoInG ON
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.Originally Posted by g4j31a5
I hope that makes sence.
WhAtHA hell Is GoInG ON
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". }
I used to be an adventurer like you... then I took an arrow to the knee.
Thanks mate that is exactly what i needed, cheers.Originally Posted by hk_mp5kpdw
WhAtHA hell Is GoInG ON