Fstream无法创建新文件 [英] Fstream fails to create new file
问题描述
我为一个项目使用了一个FileManager,所以阅读和写作对我来说不是一件麻烦事。或者,如果我没有花费所有的时间来调试它。所以,这种舒适的课程实际上给我带来了压力和时间。真棒。
问题似乎是 fstream
。
class FileManager:Utility :: Uncopyable
{
public:
FileManager();
void open(std :: string const& filename);
void close();
void read(std :: string& buffer);
void write(std :: string const& data);
private:
std :: fstream stream_;
};
非常简单。在读取功能期间,缓冲区被载入数据,数据参数是写入文件的内容。在阅读和写作之前,您必须打开文件,否则可能会冒出一个很大的例外。有点像我现在得到的。
场景:简单的命令行注册用户,然后将数据写入文件。我要求一个名字和密码。名称被复制并附加.txt(文件名)。所以它看起来像这样: 问题:打开失败。该文件永远不会被创建,在写入期间,我得到一个没有打开文件的异常。 FileManager :: open()函数: 然而,如果我事先创建了这个文件,那么打开文件。然而,当我检查时,默认的 如何才能做到这一点? 最佳方法: b I'm using a FileManager for a project so that reading and writing is less of a hassle for me. Or would be, if I didn't spend all this time debugging it. So, this comfort-class actually caused me stress and time. Awesome. The problem seems to be the Very simple. The buffer is loaded with data during the read function, the data parameter is what's to be written to file. Before reading and writing you must open the file or risk getting a big, fat exception in your face. Kind of like the one I'm getting now. Scenario: Simple command-line registering of a user, then writing the data to file. I ask for a name and password. The name is copied and appended with .txt (the filename). So it looks like this: Problem: the open fails. The file is never created, and during the write I get an exception for not having an open file. FileManager::open() function: and write However, if I create the file beforehand, then it has no troubles opening the file. Yet, when I check, the default How can I accomplish this? Best method: So the code tests for an existing file and, if not, creates it. 这篇关于Fstream无法创建新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
pre $ void SessionManager :: writeToFile(std :: string const& name,
std: :string const& password)
{
std :: string filename = name +.txt;
std :: string data;
data + = name +,+ password;
try
{
fileManager _-> open(filename);
fileManager _-> write(data);
fileManager _-> close();
}
catch(FileException& exception)
{
/ *清理它。 * /
std :: cerr<<例外。什么()<< \\\
;
throw;
$ b
$ b
void FileManager: :open(std :: string const& filename)
{
if(stream_.is_open())
stream_.close();
stream_.open(filename.c_str());
code
$ $如果(stream_.is_open())
stream_<<><>< code> void FileManager :: write(std :: string const& data)数据;
else
抛出FileException(Error。No file opened.\\\
);
}
std :: ios :: openmode
是 std :: ios :: in |的std :: IOS ::出
。当我只标记 std :: ios :: out
时,我可以创建文件,但是我想保持流处于读/写状态。
$ b
void FileManager :: open(std :: string const& filename)
{
using std :: ios_base;
if(stream_.is_open())
stream_.close();
stream_.open(filename.c_str()); // ...试试现有文件
if(!stream_.is_open())// ... else,创建新文件...
stream_.open(filename.c_str(),ios_base: :in | ios_base :: out | ios_base :: trunc);
$ b因此,代码测试一个现有的文件,如果没有,创建它。 fstream
. Before I continue further, here is the structure of my FileManager class.class FileManager : Utility::Uncopyable
{
public:
FileManager();
void open(std::string const& filename);
void close();
void read(std::string& buffer);
void write(std::string const& data);
private:
std::fstream stream_;
};
void SessionManager::writeToFile(std::string const& name,
std::string const& password)
{
std::string filename = name + ".txt";
std::string data;
data += name +", " +password;
try
{
fileManager_->open(filename);
fileManager_->write(data);
fileManager_->close();
}
catch(FileException& exception)
{
/* Clean it up. */
std::cerr << exception.what() << "\n";
throw;
}
}
void FileManager::open(std::string const& filename)
{
if(stream_.is_open())
stream_.close();
stream_.open(filename.c_str());
}
void FileManager::write(std::string const& data)
{
if(stream_.is_open())
stream_ << data;
else
throw FileException("Error. No file opened.\n");
}
std::ios::openmode
is std::ios::in | std::ios::out
. I can create the file just fine when I only tag std::ios::out
, but I want to keep the stream in a read/write state.void FileManager::open(std::string const& filename)
{
using std::ios_base;
if( stream_.is_open() )
stream_.close();
stream_.open( filename.c_str() ); // ...try existing file
if( !stream_.is_open() ) // ...else, create new file...
stream_.open(filename.c_str(), ios_base::in | ios_base::out | ios_base::trunc);
}