Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. String output to ofstream object

String output to ofstream object

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    marinme
    wrote on last edited by
    #1

    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 function fstream & operator<<(string & someString) was defined in the string class... any help?

    N M 2 Replies Last reply
    0
    • M marinme

      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 function fstream & operator<<(string & someString) was defined in the string class... any help?

      N Offline
      N Offline
      NewbieStats
      wrote on last edited by
      #2

      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. */

      M 1 Reply Last reply
      0
      • N NewbieStats

        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. */

        M Offline
        M Offline
        marinme
        wrote on last edited by
        #3

        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;
        
        1 Reply Last reply
        0
        • M marinme

          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 function fstream & operator<<(string & someString) was defined in the string class... any help?

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          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

          M 1 Reply Last reply
          0
          • M Michael Dunn

            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

            M Offline
            M Offline
            marinme
            wrote on last edited by
            #5

            thanks very much! that's what I was looking for

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups