file binary and template

Discussion in 'C++' started by opotonil, May 22, 2004.

  1. opotonil

    opotonil Guest

    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.
     
    opotonil, May 22, 2004
    #1
    1. Advertising

  2. "opotonil" <> wrote in message
    news:c8nj6l$g9f$...
    | Hi.
    |
    | class whith Function generic for write binary files:

    [snip]

    What's the question ?

    Cheers.
    Chris Val
     
    Chris \( Val \), May 22, 2004
    #2
    1. Advertising

  3. opotonil

    opotonil Guest

    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.
     
    opotonil, May 22, 2004
    #3
  4. "opotonil" <> wrote in message
    news:c8np30$s4u$...
    > 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
     
    John Harrison, May 22, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Theis
    Replies:
    2
    Views:
    503
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    568
    tom_usenet
    Jul 24, 2003
  3. Replies:
    1
    Views:
    2,150
    Gianni Mariani
    Jun 8, 2007
  4. Ron Eggler

    writing binary file (ios::binary)

    Ron Eggler, Apr 25, 2008, in forum: C++
    Replies:
    9
    Views:
    970
    James Kanze
    Apr 28, 2008
  5. Jim
    Replies:
    6
    Views:
    768
Loading...

Share This Page