Fstream无法创建新文件 [英] Fstream fails to create new file

查看:98
本文介绍了Fstream无法创建新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为一个项目使用了一个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(文件名)。所以它看起来像这样:

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

问题:打开失败。该文件永远不会被创建,在写入期间,我得到一个没有打开文件的异常。

FileManager :: open()函数:
$ 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
$ 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因此,代码测试一个现有的文件,如果没有,创建它。

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

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:

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

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:

void FileManager::open(std::string const& filename)
{
    if(stream_.is_open())
        stream_.close();

    stream_.open(filename.c_str());
}

and write

void FileManager::write(std::string const& data)
{
    if(stream_.is_open())
        stream_ << data;
    else
        throw FileException("Error. No file opened.\n");
}

However, if I create the file beforehand, then it has no troubles opening the file. Yet, when I check, the default 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.

How can I accomplish this?

解决方案

Best method:

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

So the code tests for an existing file and, if not, creates it.

这篇关于Fstream无法创建新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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