#Help with fstream in cpp

36 messages · Page 1 of 1 (latest)

wet drum
#

Hi, I'm actually creating a project and so for that I decided to create my version of file management with a class but my problem is that I added a function called "changeMode" and that's to change the open mode of the file, here the code

void personal::File::changeMode(std::ios_base::_Openmode mode)
{
    m_out->close();
    delete(m_out);
    m_out = new std::ofstream(m_outPath, mode);
}

and I get an error when I want to use the function with the parameter, I'll write down the error just bellow but the code where the error is, is here

if (m_mode == std::ios_base::app)
{
    changeMode(std::ios_base::out);
}
*m_out << finalText;
short stormBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

wet drum
#

if types are not correct it's normal I try many of them like the default std::ios::out but it don't work

#

so the error is :

C++ the object has type qualifiers that are not compatible with the member function        
    
object type is: const personal::File
halcyon oriole
#

changeMode modifies the File, but said File is const. Can't change a constant.

wet drum
#

um

rich geode
#

why do you create them with new?

wet drum
#

it's created on heap

rich geode
#

and how long is the lifetime supposed to be?

wet drum
wet drum
halcyon oriole
rich geode
wet drum
#

I send all my code with header and definitions so you all can see

rich geode
#

you have zero reasons to use heap allocations here

wet drum
#

it didn't work without

#

so idk

#

so first module.h :

#
#pragma once

#include <string>
#include <fstream>

namespace personal
{
    //create string class


    class File
    {
    public: // enums
        enum WriteMode : char
        {
            INSERT = 0,
            REPLACE
        };

    public : // methods
        /* 
        * Constructor of File
        * @param path : the path of the file you write and read in
        * @param mode : the mode of opening the file (default = std::ios::out)
        */
        File(std::string& const path, std::ios::_Openmode mode = std::ios_base::out);
        File(File& const file);

        ~File();

        /*
        Get the file's path you read in
        */
        std::string path() const { return m_path;};
        /*
        Get the file's path you write in
        */
        std::string outPath() const { return m_outPath; };

        /*
        Set the file's path you read in
        */
        void path(std::string& path);
        /*
        Set the file's path you write in
        */
        void outPath(std::string& path);

        /*
        Change the mode of the file stream you write in
        */
        void changeMode(std::ios_base::_Openmode mode);

        /*
        * Write in the file
        * @param text : you seriously ask what is this param ???
        * @param pos : the position where you write your text
        * @param mode : the writing mode ( default = WriteMode.INSERT, can be REPLACE)
        */
        void write(std::string& text, int pos = 0, WriteMode mode = INSERT) const;

    private :  // attributes
        std::ofstream* m_out;
        std::ios_base::openmode m_mode;
        std::string m_outPath;

        std::string m_path;
        std::ifstream* m_file;

        std::string m_text;
    };
}
halcyon oriole
#

You can also just use open to change the mode.

halcyon oriole
wet drum
#

File.cpp :

#include "module.h"

personal::File::File(std::string& const path, std::ios::_Openmode mode) :
    m_path(path), m_mode(mode), m_outPath(path), m_text("")
{
    m_file = new std::ifstream(path);
    m_out = new std::ofstream(path, mode);
    
}

personal::File::File(File& const file)
{
    m_path = file.m_path;
    m_out = new std::ofstream(file.m_outPath, file.m_mode);
    m_file = new std::ifstream(file.m_path);
}

personal::File::~File()
{
    delete(m_out);
    delete(m_file);
}



void personal::File::path(std::string& path)
{
    m_path = path;

    m_file->close();
    delete(m_file);
    m_file = new std::ifstream(path);
}

void personal::File::outPath(std::string& path)
{
    m_path = path;

    m_out->close();
    delete(m_out);
    m_out = new std::ofstream(path, m_mode);
}

void personal::File::changeMode(std::ios_base::_Openmode mode)
{
    m_out->close();
    delete(m_out);
    m_out = new std::ofstream(m_outPath, mode);
}

void personal::File::write(std::string& text, int pos, WriteMode mode) const
{
    std::string finalText = m_text;
    if (mode == File::INSERT)
    {
        m_out->seekp(pos);
        *m_out << text;
    }
    else if (mode == File::REPLACE)
    {
        for (int i = pos; i < text.length(); i++)
        {
            // if we replace a char
            if (finalText.length()+1 > pos+i)
            {
                finalText[i] = text[i];
            }
            //if we need to add a text at the end
            else
            {
                finalText[i] += text[i];
            }
        }

        if (m_mode == std::ios_base::app)
        {
            changeMode(std::ios_base::out);
        }
        *m_out << finalText;
    }

    // change m_text
}
#

I look up I say to you

halcyon oriole
#

void personal::File::write(std::string& text, int pos, WriteMode mode) const
That const at the end says you will not change File, so you can't call changeMode.

wet drum
#

I remove that thx

wet drum
halcyon oriole
#

Yes. Better than that constant new and delete.

wet drum
#

and yes it's solved by removing that const

halcyon oriole
#

I'm not sure if you need to close before calling open again. Probably.

wet drum
#

and with new it work well

#

!solved