file binary and template

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::eek: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::eek:ut | ios::binary);

fichero.write(reinterpret_cast<const char*>(&dato), sizeof(T));

fichero.close();

}



Thanks.
 
C

Chris \( Val \)

| Hi.
|
| class whith Function generic for write binary files:

[snip]

What's the question ?

Cheers.
Chris Val
 
O

opotonil

Hi.

The function: template <typename T> void Fichero::f_escribir(string nombre,
T dato) not write in the file and i don´t know that it is bad.

Thanks
------------------------Spanish--------------------------
Hola

La funcion: template <typename T> void Fichero::f_escribir(string nombre, T
dato) no escribe nada en el fichero y no se que es lo que falla, el dato que
tendria que guardar es una estructura.

Gracias y salu2.

PD: existe algun buen grupo de noticias, como este, en español, mi ingles es
penoso.
 
J

John Harrison

opotonil said:
Hi.

The function: template <typename T> void Fichero::f_escribir(string nombre,
T dato) not write in the file and i don´t know that it is bad.

Thanks

Try this

bool Fichero::f_existe(string nombre)
{
bool ok = false;
fichero.open(nombre.c_str(), ios::in);
if(fichero.good())
ok = true;
fichoro.clear(); // ***** clear the stream error state
fichero.close();
return ok;
}

If the file does not exist and you do not clear the error state, then
fichoro becomes unusable. You must always clear the stream after every error
if you want to use the stream again.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top