C++ fstream 写入重载的“<<”,但失败,文件仍然为空

发布于 2025-01-19 09:47:38 字数 1270 浏览 2 评论 0 原文

#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdlib>
#include <ctype.h>
#include <stdlib.h>
using namespace std;

class W_File_of_Int : public ofstream {
private:
    ofstream _fout;

public:
    W_File_of_Int(const char* nom_fic)
    {
        ofstream _fout(nom_fic, ios::out | ios::binary);
        cout << "\n\tfile of int to write is ready";
    }
    ~W_File_of_Int()
    {
        cout << "\n\tDestruction of file to write\n";
        _fout.close();
    }
    int tellp() { return ofstream::tellp(); }
    W_File_of_Int& operator<<(int i)
    {
        _fout.write((char*)&i, sizeof(int));
        return *this;
    }
};

int main()
{
    W_File_of_Int f_out("essai.fic");

    if (!f_out) {
        cerr << "\nErreur dansla creation de 'essai.fic'\n";
        return 1;
    }
    for (int i = 0; i <= 10; i++) // Ecriture de 11 entiers dans le fichier
        f_out << i;
    cout << f_out.tellp() << "\n\telements sont ecrits dans le fichier.\n";
    // affiche: 11 elements sont ecrits dans le fichier.
    f_out.close();

    return 0;
}

我尝试了很多次,例如make int to&amp; int,它只是没有

用过载的工作写入“&lt;&lt;”,但失败了,文件仍然为空。

#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdlib>
#include <ctype.h>
#include <stdlib.h>
using namespace std;

class W_File_of_Int : public ofstream {
private:
    ofstream _fout;

public:
    W_File_of_Int(const char* nom_fic)
    {
        ofstream _fout(nom_fic, ios::out | ios::binary);
        cout << "\n\tfile of int to write is ready";
    }
    ~W_File_of_Int()
    {
        cout << "\n\tDestruction of file to write\n";
        _fout.close();
    }
    int tellp() { return ofstream::tellp(); }
    W_File_of_Int& operator<<(int i)
    {
        _fout.write((char*)&i, sizeof(int));
        return *this;
    }
};

int main()
{
    W_File_of_Int f_out("essai.fic");

    if (!f_out) {
        cerr << "\nErreur dansla creation de 'essai.fic'\n";
        return 1;
    }
    for (int i = 0; i <= 10; i++) // Ecriture de 11 entiers dans le fichier
        f_out << i;
    cout << f_out.tellp() << "\n\telements sont ecrits dans le fichier.\n";
    // affiche: 11 elements sont ecrits dans le fichier.
    f_out.close();

    return 0;
}

I tried many times ,like make int to &int,it just don`t work

write with overloaded "<<",but failed, file is still empty.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扬花落满肩 2025-01-26 09:47:38

您的构造函数可以访问 3 个不同的 ofstream 实例:

  • 基类
  • 成员变量 this->_fout
  • 局部变量 _fout

您可能应该仅使用基类,为此您需要更改构造函数:

W_File_of_Int(const char* nom_fic) :
    ofstream(nom_fic, ios::out|ios::binary) // call base class constructor
{
    cout << "\n\tfile of int to write is ready";
}

Your constructor has access to 3 different ofstream instances:

  • The base class
  • The member variable this->_fout
  • The local variable _fout.

You should probably use the base class only, for which you would need to change the constructor:

W_File_of_Int(const char* nom_fic) :
    ofstream(nom_fic, ios::out|ios::binary) // call base class constructor
{
    cout << "\n\tfile of int to write is ready";
}
勿忘初心 2025-01-26 09:47:38
class W_File_of_Int :  public ofstream
{
private:ofstream _fout;
.......

在这里您创建了两个 ofstream : _fout 和基类;

你应该选择一个

class W_File_of_Int :  public ofstream
{
private:ofstream _fout;
.......

here you create two ofstream : _fout AND the base class;

you should choose one

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文