ofstream 无法打开或写入文件
ofstream doesn't open, or write to files
我已经看了几个小时了,我只知道答案很简单。不管我怎么做,似乎就是打不开文件。它是一个多类程序所以在头文件中我输入
#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 << "tt" << 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 << "tt" <<"<page>"<< endl;
PrintURL(page.Value());
PrintDescription(page.Description() );
f << "tt" <<"</page>"<< endl;
}
void XMLWriter::PrintURL(URL url){
f << "ttt<url>"<< endl;
f << "tttt" << StringUtil::EncodeToXmlCopy(url.Value()) << endl;
f << "ttt</url>"<< endl;
}
void XMLWriter::PrintDescription(string dscrptn){
f << "ttt<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 << "tt<word>" << endl;
PrintValue(ocs.Value());
ocs.ResetIterator();
while(ocs.HasNext())
PrintOccurence(*(ocs.Next()) );
f << "tt</word>"<< endl;
}
void XMLWriter::PrintValue(string s ){
f << "ttt<value>";
f << StringUtil::EncodeToXmlCopy(s);
f << "</value>"<< endl;
}
void XMLWriter::PrintOccurence(Occurence o ){
f << "ttt<occurence>" << endl;
PrintURL(o.Value()->Value());
PrintValue(o.NumOfOccur());
f << "<ttt/occurence>"<< endl;
}
void XMLWriter::PrintValue(int n ){
f << "tttt<count>";
f << n;
f << "</count>"<< endl;
}
它不会向文件写入任何内容:(但现在它正在创建一个文件,所以这是一个步骤:-D。显然我有数据结构和其他东西来支持这个,但我只需要把它写下来。提前感谢
最明显的问题是您多次打开该文件。打开文件的每个实例都有自己的文件位置和它的自己的缓冲区。另外,根据系统的不同,要么全部打开,要么全部打开第一个将失败(我认为是Windows),或者打开将截断文件,有效地擦除任何可能已写入的信息到它。您应该做的是让PrintToFile
将开放流传递给它调用的函数(递归地);每个函数都应该取一个std::ostream&
(而不是 std::ofstream&
)来接收它。
我看到的一件事是您多次打开该文件。也许那会引起麻烦。你先在PrintToFile中打开它然后在PrintBase中为append打开它。然后在PrintBase中,在调用PrintNext时再次打开它。
将ofstream作为类的成员,打开它一次,并从所有三个函数中引用它。
我会用一个简单的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();
这样可以确定文件是否成功打开。
相关文章:
- 如何在软件代码中使用ofstream创建文件
- 使用 libssh 从 SFTP 服务器下载文件并使用 C++ 将其写入 ofstream?
- C++:std::ofstream 方法 open() 在第二次迭代时擦除打开的 ifstream 文件
- 使用 ofstream 写入文本文件时断言失败
- 当 ofstream 指针位于 std::map 中时,文件写入不会发生 C++.
- BigInteger:在 C++ 中使用 ofstream 写入文件时如何将基数更改为 2?
- ofstream将额外的零字节写入Unix服务器上的文件
- 为什么我不能将 Windows 环境路径与 ofstream 一起使用来编写文本文件?
- 重定向到文件的C ++ COUT比使用Ofstream慢
- 使用 ofstream 迭代访问和写入多个文件
- std :: ofstream,覆盖错误的文件行
- 如何使用ofstream打开文件,以便其他用户可以附加/写入同一文件
- ofstream 创建一个文件但不写入
- 为什么 ofstream 无法在我的 CERN 根宏中打开文件?
- c ofstream写入变量到文本文件中
- 我想使用 ofstream 在 cpp 中创建一个文本文件
- std::ofstream 追加文件
- 无法使用 ofstream 写入 Windows 7 中的临时文件夹
- 使用std:ofstream时,将目录的副本检查到文件中
- 读取文件(ifstream)和写入文件(ofstream)-编码问题