我已经看了好几个小时了,我只知道答案很简单。似乎无论我做什么,我都无法打开文件。这是一个多类程序,所以在我的标题中
#include <iostream>
#include < fstream>
class A{
string path;
A(string p): path(p){}
...
...
void PrintToFile();
void PrintBase();
void PrintNext();
...
...
};
在我的cpp文件中
#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()
}
我玩过构造函数上的标志和打开的命令。一旦它设法打开一个文件,但它从未向该文件写入任何内容。如果您有任何见解,我将不胜感激。
编辑感谢所有帮助家伙,看起来我试图用“”打开一个文件。但是即使现在我已经理顺了这一点,我的代码也没有写入那个打开的文件。我检查了我的权限,我正在执行 chmod a+rwx ......这里是更详细的代码。
#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 文件
#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 = π
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;
}
它不会向文件写入任何内容:(但现在它正在创建一个文件,所以这是一个步骤:-D。显然我有一个数据结构和其他东西支持它,但我只需要让它写入。谢谢进步