ofstream不打开,或写入文件 [英] ofstream doesn't open, or write to files

查看:165
本文介绍了ofstream不打开,或写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

和在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()
}

标志和构造函数以及打开的命令。一旦它设法打开一个文件,但它从来没有写任何文件。如果你有任何见解,我非常感谢。

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.

感谢所有的帮助,看起来像我试图打开一个带有的文件。但是即使现在我已经弄清楚了,我的代码不是写入那个打开的文件。我检查了我的权限,我正在做chmod a + rwx ...这里的代码更详细。

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档案

#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;
}

它不会写入任何东西到文件:(但现在它正在创建一个文件使得一个步骤:-D。
显然我有一个数据结构和其他事情支持,但我只需要得到它写。提前感谢

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

推荐答案

最明显的问题是你打开文件多次
打开文件的每个实例都有自己的文件位置和
此外,根据系统,或者所有打开的但
第一将失败(Windows,我想),或者打开将截断
文件,有效地擦除任何信息,可能已经你应该做的是有 PrintToFile 将开放流传递给
它调用的函数(递归);每个函数应该
获取 std :: ostream& not std :: ofstream& )接收它。

The most obvious problem is that you are opening the file several times. Each instance of the open file will have its own file position and its own buffer. In addition, depending on the system, either all open's but the first will fail (Windows, I think), or the open will truncate the file, effectively erasing any information that might have been written to it. What you should do is have PrintToFile pass the open stream to the functions it calls (recursively); each of these functions should take a std::ostream& (not std::ofstream&) to receive it.

这篇关于ofstream不打开,或写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆