O
opotonil
Hi.
class whith Function generic for write binary files:
#include <fstream>
#include <iostream>
using namespace std;
#ifndef _FICHERO_H_
#define _FICHERO_H_
class Fichero{
private:
fstream fichero;
public:
bool f_existe(string nombre);
void f_crear(string nombre, bool oculto);
template <typename T> void f_escribir(string nombre, T dato);
};
#endif
bool Fichero::f_existe(string nombre){
bool ok = false;
fichero.open(nombre.c_str(), ios::in);
if(fichero.good())
ok = true;
fichero.close();
return ok;
}
void Fichero::f_crear(string nombre, bool oculto = false){
string auxiliar;
fichero.open(nombre.c_str(), ios:ut);
fichero.close();
if(oculto){
auxiliar = "attrib +h " + nombre;
system(auxiliar.c_str());
}
}
/******************************************************************NOT
WRITE***********************************************************************
****************************/
template <typename T> void Fichero::f_escribir(string nombre, T dato){
fichero.open(nombre.c_str(), ios:ut | ios::binary);
fichero.write(reinterpret_cast<const char*>(&dato), sizeof(T));
fichero.close();
}
Thanks.
class whith Function generic for write binary files:
#include <fstream>
#include <iostream>
using namespace std;
#ifndef _FICHERO_H_
#define _FICHERO_H_
class Fichero{
private:
fstream fichero;
public:
bool f_existe(string nombre);
void f_crear(string nombre, bool oculto);
template <typename T> void f_escribir(string nombre, T dato);
};
#endif
bool Fichero::f_existe(string nombre){
bool ok = false;
fichero.open(nombre.c_str(), ios::in);
if(fichero.good())
ok = true;
fichero.close();
return ok;
}
void Fichero::f_crear(string nombre, bool oculto = false){
string auxiliar;
fichero.open(nombre.c_str(), ios:ut);
fichero.close();
if(oculto){
auxiliar = "attrib +h " + nombre;
system(auxiliar.c_str());
}
}
/******************************************************************NOT
WRITE***********************************************************************
****************************/
template <typename T> void Fichero::f_escribir(string nombre, T dato){
fichero.open(nombre.c_str(), ios:ut | ios::binary);
fichero.write(reinterpret_cast<const char*>(&dato), sizeof(T));
fichero.close();
}
Thanks.