0

I'm currently working with std::fstream and have the following class: (Note that the file is opened on constructor and should stay open during all read / write operation, only close when destructed).

MyFileClass
{
public:
   MyFileClass( const std::string& file_name ) { m_file.open( file_name ) };
   ~MyFileClass() { m_file.close() };
   bool read( std::string& content );
   bool write( std::string& data );
private:
   std::fstream m_file;
}

Now I have some sample code:

MyFileClass sample_file;
sample_file.write("123456");
sample_file.write("abc");

Result will be "abc456" because when the stream is open and we're writing with truncate mode it will always write on top of what's currently in there.

What I would like to have is to clean up everytime before we write so at the end I'll have only what's newest in there, in this case "abc".

And the current design is if the file is not there, it will be created only on write, but not on read (read will return error code if file is not present).

My write function is:

bool
MyFileClass::write( const std::string& data )
{   
    m_file.seekg( 0 );
    if ( !m_file.fail( ) )
    {
        m_file << data << std::flush;
    }

    return m_file.fail( ) ? true : false;
}

Is there any way to clear the current content of the file before flushing the data?

mappa
  • 29
  • 4
  • 1
    C++ doesn't provide a way to truncate an already open file. See http://stackoverflow.com/questions/20809113/how-to-truncate-a-file-while-it-is-open-with-fstream – Barmar Oct 11 '16 at 17:49
  • Other than reopening the file, no. – UKMonkey Oct 11 '16 at 17:52

1 Answers1

0

to be able to write to the end of file use flag ios::app instead.

your code is full of mistakes:

1- you also trying to write a class string to file which is not correct. if you want to do so use serialization. in your case just convert it to constant character string.

2- your write and read functions defined to take a reference to string but you pass by value! (passing "12345" and "abc")

3- why you seek input pointer? (seekg)? as long as you are trying to write??!! you may meant seekp() seeking output pointer; even in this case for what reason to do so? if you want to append text to the end use ios::app at the opening file. if you want to clearing the content at any write operation then you should have two file streams one for reading and other for writing so the one for writing uses flag ios::out | ios::trunc. because ios::trunc does nothing in reading/writing mode.

4- if you really want to pass by reference then you must declare string objects in main passing to them values ("abc" and "12345" for each one) then pass these strings to write and read not the values.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class MyFileClass
{
public:
    MyFileClass(  std::string file_name );
   ~MyFileClass() { m_file.close(); }
   bool read ( std::string content );
   bool write( std::string data );
private:
   std::fstream m_file;
};

MyFileClass::MyFileClass( std::string file_name )
{
    m_file.open( file_name.c_str(), ios::out | ios::in | ios::app);
    if(!m_file)
        cout << "Failed to open file!" << endl;
}

bool MyFileClass::write( const std::string data )
{   
    if ( !m_file.fail( ) )
    {
        m_file << data.c_str() << std::flush;
    }

    return m_file.fail( ) ? true : false;
}

int main()
{

    std::string sFile = "data.dat";

    MyFileClass sample_file(sFile);

    sample_file.write("123456");
    sample_file.write("abc");

    return 0;
}
Raindrop7
  • 3,889
  • 3
  • 16
  • 27
  • Hi, thanks for your comment. It was my mistake with all the const and stuff but it's just minor sample and not related to the main question at all. – mappa Oct 12 '16 at 08:01
  • Accidently hit enter. The answer in short I got was I can't truncate and already open file. Two streams suggestion is not an option ( – mappa Oct 12 '16 at 08:03
  • Again....Okay so your point 1,2,4 are all my mistakes and it's not what I want to ask ( just an example to demonstrate the idea, code doesn't need to run or compile ). Point 3- I have 1 file already open and it will stay open, that's the requirement. Another stream or open it again to reassign new mode is not an option. Barmar's links described the problem and current limitation well enough. – mappa Oct 12 '16 at 08:07