C++ 流不';无法打开或写入文件

C++ 流不';无法打开或写入文件,c++,ofstream,C++,Ofstream,我已经看了几个小时了,我只知道答案很简单。似乎无论我做什么我都无法打开一个文件。这是一个多类程序,所以在标题中我有 #include <iostream> #include < fstream> class A{ string path; A(string p): path(p){} ... ... void PrintToFile(); void PrintBase(); void PrintNext(); ... ... };

我已经看了几个小时了,我只知道答案很简单。似乎无论我做什么我都无法打开一个文件。这是一个多类程序,所以在标题中我有

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

  A(string p): path(p){}
  ...
  ...
  void PrintToFile();
  void PrintBase();
  void PrintNext();
  ...
  ...
};
#包括
#包括
甲级{
字符串路径;
A(字符串p):路径(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()
}
#包括“A.h”
void A::PrintToFile(){
流f(path.c_str(),ios::out);
断言(f.is_open);

f我看到的一件事是,您正在多次打开该文件。这可能会造成麻烦。您首先在PrintToFile中打开它,然后在PrintBase中打开它进行追加。然后在PrintBase中,您在调用PrintNext时再次打开它


将ofstream作为类的一个成员,打开它一次并从所有三个函数中引用它。

如果:

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();

通过这种方式,您可以确定文件是否成功打开。

最明显的问题是您多次打开文件。 打开文件的每个实例都有其自己的文件位置和位置 自己的缓冲区。此外,根据系统的不同,所有打开的 第一个将失败(我认为是Windows),或者打开将截断 文件,有效地删除可能已写入的任何信息 您应该做的是让
PrintToFile
将打开的流传递给 它(递归地)调用的函数;每个函数都应该
拿一个
std::ostream&
(而不是
std::ofstream&
)来接收它。

该代码无法编译,我们不知道您试图如何使用它。您应该提供一个可以构建的完整示例。您可能也不能同时打开该文件(您尝试在每个方法中打开它).is_open是一个方法而不是成员。您是否已检查您是否具有向您认为要向其写入内容所需的权限(是否已检查您正在向您认为要向其写入内容的内容写入内容)?您在windows btw上吗?在打开文件之前,为路径放置一些调试打印。除此之外,它应该可以工作。这不是您的实际程序。它有许多语法错误,无法编译。此外,一旦这些错误得到修复,它会将内容打印到文件中。是否可能您没有对工作目录/文件的写入权限,或者驱动器已满?或者文件是用锁定文件的程序打开的?@MooingDuck:我不应该同时使用,多次使用可能会更好。以我有限的经验(MSVC)ofstream以独占写入模式打开,因此,虽然第一个将成功,但第二个和第三个将无法打开该文件,因此认为这是一个可能的问题,但应该有更多的“可能”而不是“可能”。
#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;
}
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();