String output to ofstream object
-
Hello! I'm having a problem writing string objects to a file. I just learned file I/O and am not quite sure what the problem is. I am using a string object
(basic_string<char *> )
and it doesn't seem to like what I am doing. here is some sample code:#include <fstream> using namespace std; #include <cstring> ofstream myFile("file.txt"); int main() { string myBuffer("Some text");//from the std namespace myFile.write(myBuffer, myBuffer.size()); myFile.close(); return 0; }
this does not like my string going into the file. I have also tried the extraction operation
<<
with it, but it says it is not defined... I thought the functionfstream & operator<<(string & someString)
was defined in the string class... any help? -
Hello! I'm having a problem writing string objects to a file. I just learned file I/O and am not quite sure what the problem is. I am using a string object
(basic_string<char *> )
and it doesn't seem to like what I am doing. here is some sample code:#include <fstream> using namespace std; #include <cstring> ofstream myFile("file.txt"); int main() { string myBuffer("Some text");//from the std namespace myFile.write(myBuffer, myBuffer.size()); myFile.close(); return 0; }
this does not like my string going into the file. I have also tried the extraction operation
<<
with it, but it says it is not defined... I thought the functionfstream & operator<<(string & someString)
was defined in the string class... any help?http://www.codeproject.com/script/comments/forums.asp?forumid=1647&select=1071428&df=100&tid=1071234#xx1071428xx[^] Read the two post ive made... using MFC and Windows App... There are alot of ways to i/o... If none of those suit ur needs then ill try and help you with another... /* Just a Human Trying to Live in a Computers World. */
-
http://www.codeproject.com/script/comments/forums.asp?forumid=1647&select=1071428&df=100&tid=1071234#xx1071428xx[^] Read the two post ive made... using MFC and Windows App... There are alot of ways to i/o... If none of those suit ur needs then ill try and help you with another... /* Just a Human Trying to Live in a Computers World. */
well, that would work using the CString class, but I'm trying to use the typedef'd string class from basic_string<char *> I believe it is in the <string> header file... I've never used the CString class(that I know of, and I'm trying to stick to using the one I'm used to.. It doesn't work using
#include <string> basic_string<char *> myString myOutputFile << myString;
-
Hello! I'm having a problem writing string objects to a file. I just learned file I/O and am not quite sure what the problem is. I am using a string object
(basic_string<char *> )
and it doesn't seem to like what I am doing. here is some sample code:#include <fstream> using namespace std; #include <cstring> ofstream myFile("file.txt"); int main() { string myBuffer("Some text");//from the std namespace myFile.write(myBuffer, myBuffer.size()); myFile.close(); return 0; }
this does not like my string going into the file. I have also tried the extraction operation
<<
with it, but it says it is not defined... I thought the functionfstream & operator<<(string & someString)
was defined in the string class... any help?myFile.write(myBuffer, myBuffer.size());
That won't do what you want. You want to write the contents of the string, not the object itself, so do:
myFile.write(myBuffer.c_str(), myBuffer.size());
--Mike-- LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD
-
myFile.write(myBuffer, myBuffer.size());
That won't do what you want. You want to write the contents of the string, not the object itself, so do:
myFile.write(myBuffer.c_str(), myBuffer.size());
--Mike-- LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD