Writing to a file.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jajjo
    New Member
    • May 2009
    • 26

    Writing to a file.

    I have an odd problem.

    Code:
    m_myStream.open("Highscores.txt", ios_base::out);
    
    	if (m_myStream.is_open())
    	{
    		m_myStream << "Hello";
    	}
    
    	m_myStream.close();
    This code doesn't work for me.

    m_myStream is declared as an fstream. Is there any special rule about when and/or where you can call these write functions? It's odd because the all it is doing is erasing whatever is in the document and not writing anything.

    I've tried it with an ofstream too, but taht didn't work either.

    Am I not seeing something glaringly obvious?
  • Jajjo
    New Member
    • May 2009
    • 26

    #2
    Ugh. I always do this.

    Work for a couple hours on something, ask here how to fic it, then fix it straight away.

    It now works with ofstream... but not fstream... I'm not sure why that's happening.

    Comment

    • Rafael Justo
      New Member
      • Mar 2007
      • 9

      #3
      C++ - i/o

      The following code is going to overwrite any existing file called "file.txt".

      Code:
      #include <fstream>
      using std::ofstream;
      using std::endl;
      
      ofstream fileStream("file.txt");
      if (fileStream.good() == true) {
        fileStream << "This is a test message!" << endl;
        fileStream.close();
      }

      Comment

      Working...