title: ofstream doesn't open, or write to files
body: <p>I've been looking at this for hours and I just know the answer is simple. It seems no matter what I do I cannot open a file. It's a multi-class program so in the header I have</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt; fstream&gt;
  class A{
    string path;
  
    A(string p): path(p){}
    ...
    ...
    void PrintToFile();
    void PrintBase();
    void PrintNext();
    ...
    ...
  };
  </code></pre>
  <p>and in the cpp file I have</p>
  <pre><code>#include &quot;A.h&quot;
  
  void A::PrintToFile(){
  
    ofstream f(path.c_str(), ios::out);
    assert(f.is_open);
  
    f &lt;&lt; &quot;markuptext&quot; &lt;&lt; endl;
    PrintBase();
    f &lt;&lt; &quot;endtag&quot; &lt;&lt; endl;
    f.close();
  }
  
  
  void A::PrintBase(){
  
    ofstream f(path.c_str(), ios::app);
    assert(f.is_open);
  
    f &lt;&lt; &quot;markuptext&quot; &lt;&lt; endl;
    f &lt;&lt; somevale &lt;&lt; endl;
     PrintNext();
    f &lt;&lt; &quot;endtag&quot; &lt;&lt; endl;
    f.close()
  }
  
  void A::PrintNext(){
  
    ofstream f (path.c_str(), ios::app);
    assert(f.is_open);
  
    f &lt;&lt; &quot;markuptext&quot; &lt;&lt; endl;
    f &lt;&lt; somevalue &lt;&lt; endl;
    f &lt;&lt; &quot;endtag&quot; &lt;&lt; endl;
    f.close()
  }
  </code></pre>
  <p>I've played around with the flags on the constructors and with the open commands as well. And once it managed to open a file, but it never wrote anything to the file. If you have any insights I'd much appreciate it.</p>
  edit
  <p>Thanks for all the help guys, looks like I was trying to open a file with &quot;&quot;. But even now after I've got that straightened out, my code is not writing to that open file. I checked my permissions and I'm doing chmod a+rwx... well here's the code in more detail.</p>
  <pre><code>#ifndef XML_WRITER_H
  #define XML_WRITER_H
  
  #include &quot;WordIndex.h&quot;
  #include &quot;PageIndex.h&quot;
  #include &quot;StringUtil.h&quot;
  #include &quot;CS240Exception.h&quot;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  
  
  /* prints out the wordIndex to an xml file
  */
  
  class XMLWriter{
  private:
      WordIndex * wIndex;
      PageIndex * pIndex;
      URL baseurl;
      //const char * file;
      ofstream f;
  public:
  
    XMLWriter();
    XMLWriter(string base);
    XMLWriter(XMLWriter &amp; other){
        assert(&amp;other != NULL);
        Init(other);
    }
    XMLWriter &amp; operator =(XMLWriter &amp; other){
        Free();
        Init(other);
    }
    ~XMLWriter(){
        Free();
    }
  
    void Load(WordIndex &amp; wi, PageIndex &amp; pi);
  
  
    //prints to the file
    void Print(char * ofile);
  
  private:
    void Init(XMLWriter &amp; other){
      baseurl = other.baseurl;
      wIndex = other.wIndex;
      pIndex = other.pIndex;
  
    }
    void Free(){
    }
  
    void PrintWebsite();
    void PrintStartURL();
    void PrintPages();
    void PrintIndex();
    void PrintWord(OccurenceSet ocs);
    void PrintValue(string s);
    void PrintOccurence(Occurence o);
    void PrintPage(Page &amp; page );
    void PrintDescription(string dscrptn );
    void PrintValue(int n );
    void PrintURL(URL url );
  
  };
  #endif
  </code></pre>
  <p>.cpp file</p>
  <pre><code>#include &quot;XMLWriter.h&quot;
  
  XMLWriter::XMLWriter(){
  }
  
  XMLWriter::XMLWriter( string base): baseurl(base){
  //cout &lt;&lt; &quot;filename : &quot; &lt;&lt; filename &lt;&lt; endl;
  //file =  filename.c_str();
  //cout &lt;&lt; &quot;file : &quot; &lt;&lt; *file &lt;&lt; endl;
  }
  
  
  void XMLWriter::Load(WordIndex &amp; wi, PageIndex &amp; pi){
  wIndex = &amp;wi;
  pIndex = &amp;pi;
  wIndex-&gt;ResetIterator();
  pIndex-&gt;ResetIterator();
  }
  
  
  void XMLWriter::Print(char * filename){
  
      cout &lt;&lt; filename &lt;&lt; endl;
      ofstream f(filename);
      if(!f){
        cout &lt;&lt; &quot;file : &quot; &lt;&lt; filename;
        throw CS240Exception(&quot;could not open the file for writing&quot;);
      }
      PrintWebsite();
      f.close();
  
  }
  //private methods
  //
  void XMLWriter::PrintWebsite(){
  
  
      f &lt;&lt;&quot;&lt;website&gt;\n&quot;;
      PrintStartURL();
      PrintPages();
      PrintIndex();
      f &lt;&lt; &quot;&lt;/website&gt;&quot; &lt;&lt; endl;
  }
  
  // startURL
  //
  void XMLWriter::PrintStartURL( ){
  
      f &lt;&lt; &quot;\t&quot; &lt;&lt; &quot;&lt;start-url&gt;&quot;&lt;&lt; endl;
      string val = baseurl.Value();
      StringUtil::EncodeToXml(val);
      f &lt;&lt; &quot;\t\t&quot; &lt;&lt; val &lt;&lt; endl;
      f &lt;&lt; &quot;\t&quot; &lt;&lt; &quot;&lt;/start-url&gt;&quot;&lt;&lt; endl;
  
  
  }
  
  //pages
  //
  void XMLWriter::PrintPages(){
  
      f &lt;&lt; &quot;\t&quot; &lt;&lt; &quot;&lt;pages&gt;&quot;&lt;&lt; &quot;\n&quot;;
      while(pIndex-&gt;HasNext())
      PrintPage(*(pIndex-&gt;Next()));
      f &lt;&lt; &quot;\t&quot; &lt;&lt;&quot;&lt;/pages&gt;&quot;&lt;&lt;  '\n';
  
  }
  void XMLWriter::PrintPage(Page &amp; page ){
  
      f &lt;&lt; &quot;\t\t&quot; &lt;&lt;&quot;&lt;page&gt;&quot;&lt;&lt; endl;
      PrintURL(page.Value());
      PrintDescription(page.Description() );
      f &lt;&lt; &quot;\t\t&quot; &lt;&lt;&quot;&lt;/page&gt;&quot;&lt;&lt; endl;
  }
  void XMLWriter::PrintURL(URL url){
      f &lt;&lt; &quot;\t\t\t&lt;url&gt;&quot;&lt;&lt; endl;
      f &lt;&lt; &quot;\t\t\t\t&quot; &lt;&lt; StringUtil::EncodeToXmlCopy(url.Value()) &lt;&lt; endl;
      f &lt;&lt; &quot;\t\t\t&lt;/url&gt;&quot;&lt;&lt; endl;
  
  }
  void XMLWriter::PrintDescription(string dscrptn){
      f &lt;&lt; &quot;\t\t\t&lt;description&gt;&quot;;
      f &lt;&lt; StringUtil::EncodeToXmlCopy(dscrptn);
      f &lt;&lt; &quot;&lt;/description&gt;&quot;&lt;&lt; endl;
  }
  
  //index
  //
  void XMLWriter::PrintIndex(){
  
      f &lt;&lt; &quot;\t&lt;index&gt;&quot;&lt;&lt; endl;
      while(wIndex-&gt;HasNext())
          PrintWord(*(wIndex-&gt;Next()) );
      f &lt;&lt; &quot;\t&lt;/index&gt;&quot;&lt;&lt; endl;
  
  }
  void XMLWriter::PrintWord(OccurenceSet ocs ){
      f &lt;&lt; &quot;\t\t&lt;word&gt;&quot; &lt;&lt; endl;
      PrintValue(ocs.Value());
      ocs.ResetIterator();
      while(ocs.HasNext())
          PrintOccurence(*(ocs.Next()) );
      f &lt;&lt; &quot;\t\t&lt;/word&gt;&quot;&lt;&lt; endl;
  }
  void XMLWriter::PrintValue(string s ){
      f &lt;&lt; &quot;\t\t\t&lt;value&gt;&quot;;
      f &lt;&lt; StringUtil::EncodeToXmlCopy(s);
      f &lt;&lt; &quot;&lt;/value&gt;&quot;&lt;&lt; endl;
  
  }
  
  void XMLWriter::PrintOccurence(Occurence o ){
  
      f &lt;&lt; &quot;\t\t\t&lt;occurence&gt;&quot; &lt;&lt; endl;
      PrintURL(o.Value()-&gt;Value());
      PrintValue(o.NumOfOccur());
      f &lt;&lt; &quot;&lt;\t\t\t/occurence&gt;&quot;&lt;&lt; endl;
  
  }
  void XMLWriter::PrintValue(int n ){
  
      f &lt;&lt; &quot;\t\t\t\t&lt;count&gt;&quot;;
      f &lt;&lt; n;
      f &lt;&lt; &quot;&lt;/count&gt;&quot;&lt;&lt; endl;
  }
  </code></pre>
  <p>it won't write anything to the file :( but now it is creating a file so thats a step :-D.
  obviously I have a data structures and other things backing this up, but I just need to get it writing. Thanks in advance</p>