2

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

#include <iostream>
#include < fstream>
class A{
  string path;

  A(string p): path(p){}
  ...
  ...
  void PrintToFile();
  void PrintBase();
  void PrintNext();
  ...
  ...
};

and in the cpp file I have

#include "A.h"

void A::PrintToFile(){

  ofstream f(path.c_str(), ios::out);
  assert(f.is_open);

  f << "markuptext" << endl;
  PrintBase();
  f << "endtag" << endl;
  f.close();
}


void A::PrintBase(){

  ofstream f(path.c_str(), ios::app);
  assert(f.is_open);

  f << "markuptext" << endl;
  f << somevale << endl;
   PrintNext();
  f << "endtag" << endl;
  f.close()
}

void A::PrintNext(){

  ofstream f (path.c_str(), ios::app);
  assert(f.is_open);

  f << "markuptext" << endl;
  f << somevalue << endl;
  f << "endtag" << endl;
  f.close()
}

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.

edit

Thanks for all the help guys, looks like I was trying to open a file with "". 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.

#ifndef XML_WRITER_H
#define XML_WRITER_H

#include "WordIndex.h"
#include "PageIndex.h"
#include "StringUtil.h"
#include "CS240Exception.h"
#include <iostream>
#include <fstream>



/* 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 & other){
      assert(&other != NULL);
      Init(other);
  }
  XMLWriter & operator =(XMLWriter & other){
      Free();
      Init(other);
  }
  ~XMLWriter(){
      Free();
  }

  void Load(WordIndex & wi, PageIndex & pi);


  //prints to the file
  void Print(char * ofile);

private:
  void Init(XMLWriter & 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 & page );
  void PrintDescription(string dscrptn );
  void PrintValue(int n );
  void PrintURL(URL url );

};
#endif

.cpp file

#include "XMLWriter.h"

XMLWriter::XMLWriter(){
}

XMLWriter::XMLWriter( string base): baseurl(base){
//cout << "filename : " << filename << endl;
//file =  filename.c_str();
//cout << "file : " << *file << endl;
}


void XMLWriter::Load(WordIndex & wi, PageIndex & pi){
wIndex = &wi;
pIndex = &pi;
wIndex->ResetIterator();
pIndex->ResetIterator();
}


void XMLWriter::Print(char * filename){

    cout << filename << endl;
    ofstream f(filename);
    if(!f){
      cout << "file : " << filename;
      throw CS240Exception("could not open the file for writing");
    }
    PrintWebsite();
    f.close();

}
//private methods
//
void XMLWriter::PrintWebsite(){


    f <<"<website>\n";
    PrintStartURL();
    PrintPages();
    PrintIndex();
    f << "</website>" << endl;
}

// startURL
//
void XMLWriter::PrintStartURL( ){

    f << "\t" << "<start-url>"<< endl;
    string val = baseurl.Value();
    StringUtil::EncodeToXml(val);
    f << "\t\t" << val << endl;
    f << "\t" << "</start-url>"<< endl;


}

//pages
//
void XMLWriter::PrintPages(){

    f << "\t" << "<pages>"<< "\n";
    while(pIndex->HasNext())
    PrintPage(*(pIndex->Next()));
    f << "\t" <<"</pages>"<<  '\n';

}
void XMLWriter::PrintPage(Page & page ){

    f << "\t\t" <<"<page>"<< endl;
    PrintURL(page.Value());
    PrintDescription(page.Description() );
    f << "\t\t" <<"</page>"<< endl;
}
void XMLWriter::PrintURL(URL url){
    f << "\t\t\t<url>"<< endl;
    f << "\t\t\t\t" << StringUtil::EncodeToXmlCopy(url.Value()) << endl;
    f << "\t\t\t</url>"<< endl;

}
void XMLWriter::PrintDescription(string dscrptn){
    f << "\t\t\t<description>";
    f << StringUtil::EncodeToXmlCopy(dscrptn);
    f << "</description>"<< endl;
}

//index
//
void XMLWriter::PrintIndex(){

    f << "\t<index>"<< endl;
    while(wIndex->HasNext())
        PrintWord(*(wIndex->Next()) );
    f << "\t</index>"<< endl;

}
void XMLWriter::PrintWord(OccurenceSet ocs ){
    f << "\t\t<word>" << endl;
    PrintValue(ocs.Value());
    ocs.ResetIterator();
    while(ocs.HasNext())
        PrintOccurence(*(ocs.Next()) );
    f << "\t\t</word>"<< endl;
}
void XMLWriter::PrintValue(string s ){
    f << "\t\t\t<value>";
    f << StringUtil::EncodeToXmlCopy(s);
    f << "</value>"<< endl;

}

void XMLWriter::PrintOccurence(Occurence o ){

    f << "\t\t\t<occurence>" << endl;
    PrintURL(o.Value()->Value());
    PrintValue(o.NumOfOccur());
    f << "<\t\t\t/occurence>"<< endl;

}
void XMLWriter::PrintValue(int n ){

    f << "\t\t\t\t<count>";
    f << n;
    f << "</count>"<< endl;
}

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

4

3 に答える 3

4

最も明白な問題は、ファイルを数回開いていることです。開いているファイルの各インスタンスには、独自のファイル位置と独自のバッファーがあります。さらに、システムによっては、最初のオープンを除くすべてのオープンが失敗するか (Windows だと思います)、オープンによってファイルが切り捨てられ、書き込まれた可能性のある情報が効果的に消去されます。あなたがすべきことはPrintToFile、開いているストリームをそれが呼び出す関数に(再帰的に)渡すことです。std::ostream&これらの各関数は、それを受け取るために( not std::ofstream& ) を取る必要があります。

于 2011-11-01T18:12:26.580 に答える
0

私が見る 1 つのことは、ファイルを数回開いていることです。たぶんそれはトラブルを引き起こすでしょう。最初に PrintToFile で開き、まだ開いている間に PrintBase に追加するために開きます。次に、PrintBase で、PrintNext の呼び出しでもう一度開きます。

ofstream をクラスのメンバーとして配置し、一度開いて、3 つの関数すべてから参照します。

于 2011-11-01T17:47:55.150 に答える
0

私は単純な if を使用します:

ofstream f (path.c_str(), ios::app);
if(!f)
{
   throw std::exception(std::string("Could not open file : " + path + "for writing"));
}
//write to file here
f.close();

このようにして、ファイルが正常に開かれたかどうかを確認できます。

于 2011-11-01T17:50:02.597 に答える