ofstream 无法打开或写入文件
我已经研究这个问题几个小时了,我只知道答案很简单。似乎无论我做什么都无法打开文件。这是一个多类程序,因此在我的标头
#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()
}
我使用了构造函数上的标志以及打开的命令。一旦它成功打开一个文件,但它从未向该文件写入任何内容。如果您有任何见解,我将不胜感激。
edit感谢所有帮助人员,看起来我正在尝试使用“”打开文件。但即使现在我已经解决了这个问题,我的代码也没有写入该打开的文件。我检查了我的权限,并且正在执行 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。 显然我有数据结构和其他东西支持这一点,但我只需要写它。提前致谢
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 = π
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最明显的问题是您多次打开该文件。
打开文件的每个实例都有自己的文件位置及其
自己的缓冲区。另外,根据系统的不同,要么全部打开,要么
第一个会失败(我认为是 Windows),或者 open 会截断
文件,有效地删除可能已写入的任何信息
到它。您应该做的是将
PrintToFile
将打开的流传递给它调用的函数(递归);这些功能中的每一个都应该
使用
std::ostream&
(不是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 tothe functions it calls (recursively); each of these functions should
take a
std::ostream&
(notstd::ofstream&
) to receive it.我看到的一件事是您多次打开该文件。也许这会带来麻烦。您首先在 PrintToFile 中打开它,然后在它仍然打开时将其打开以在 PrintBase 中追加。然后,在 PrintBase 中,您可以在调用 PrintNext 时再次打开它。
将 ofstream 作为类的成员,打开一次并从所有三个函数中引用它。
One thing I see is that you are opening the file several times. Maybe that would cause trouble. You open it first in PrintToFile then while it is still open there you open it for append in PrintBase. Then while in PrintBase you open it yet again in the call to PrintNext.
Put the ofstream as a member of the class, open it once and refer to it from all three functions.
我会使用一个简单的 if :
这样您就可以确定文件是否成功打开。
I would use a simple if :
This way you can be sure if the file was opened succesfully or not.