How can I use std :: basic_filebuf from fstream to write to a file in c ++

3

I have been looking for both English and Spanish, and I do not get information that I understand about it; I try to do the following:

#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;

int main() {
    basic_filebuf <char> * buferDeArchivoBasico;    //Quiero entender el uso de basic_filbuf.
    //ofstream * buferDeArchivoBasico;              //con este Sí funciona
    buferDeArchivoBasico->open("archivo.txt",ios_base::out);

    *buferDeArchivoBasico << "intento escribir esto en el archivo"; //Este es el problema

    buferDeArchivoBasico->close();
    return 0;
    system("pause");
}

What is basic_filebuf for? and if possible an example please.

    
asked by Armando 17.04.2018 в 04:46
source

2 answers

3

C ++ offers different data read / write abstractions, in general these abstractions are separated into buffers (buffer) and flows ( stream ).

There are conceptual nuances between a buffer and a flow, in general:

  • Buffers are usually an intermediate and temporary step in further processing.
  • Flows are usually a direct and continuous input of data.

Based on these different concepts, the class basic_filebuf offers utilities to manage the buffer (advance, rewind, synchronize ...).

  

What is the basic_filebuf for?

To begin with, you are not expected to use std::basic_filebuf directly if not any of your aliases: filebuf with char as underlying type or wfilebuf with wchar_t as underlying type.

Its usefulness is to treat files (file) as data buffers, and therefore it offers raw data reading and writing tools but it lacks reading and writing tools with formatted data, for this kind of utility you were probably looking for std::basic_fstream that if offers the data insertion operator ( operator << ) of which std::basic_filebuf lacks.

  

Can you give me an example in code that uses filebuf ?

In the Microsoft documentation about basic_filebuf you have a Hex Dump implemented with that class. But if you think a very complicated example, I leave an example of reading and writing:

template <typename T>
void escribir(T valor)
{
    std::cout << "Escribir " << valor << '\n';
    std::filebuf archivo;
    if (archivo.open("archivo.bin", std::ios_base::out | std::ios_base::binary))
    {
        archivo.sputn(reinterpret_cast<const char *>(&valor), sizeof(valor));
    }
}

template <typename T>
void leer(T &valor)
{
    std::cout << "Leer sobre " << &valor;
    std::filebuf archivo;
    if (archivo.open("archivo.bin", std::ios_base::in | std::ios_base::binary))
    {
        archivo.sgetn(reinterpret_cast<char *>(&valor), sizeof(valor));
    }
    std::cout << " leido " << valor << '\n';
}

You can see the code working in Wandbox 三 へ (へ ਊ) へ ハ ッ ハ ッ .

    
answered by 17.04.2018 / 08:16
source
2
  

What is basic_filebuf for?

basic_filebuf is a low level buffer. In fact, it is rather designed to be a base class for the development of higher level interfaces ... as we can see, for example, here .

That it is of low level implies that its interface is quite short and without too many facilities ... those facilities will be provided by the classes that inherit from basic_filebuf .

Regarding your code, note that buferDeArchivoBasico is a pointer ... that you have not initialized !!!! . The strange thing is that you have not failed here:

buferDeArchivoBasico->open("archivo.txt",ios_base::out);

Try using the object by value ... you ensure its initialization and you forget to release the memory later:

basic_filebuf <char> buferDeArchivoBasico;
buferDeArchivoBasico.open("archivo.txt",ios_base::out);

// ...

buferDeArchivoBasico.close();

On the other hand, if we review the documentation we see that this class does not have an overloaded insertion operator . We can solve it easily by providing our own overload:

template<class T>
basic_filebuf<T>& operator<<(basic_filebuf<T>& out, std::string const& str)
{
  out.sputn(str.c_str(),str.length());
  return out;
}

And with this your code should already work correctly.

    
answered by 17.04.2018 в 07:57