Mince ! Mon flux fstream fonctionne très mal

Signaler
Messages postés
9
Date d'inscription
mercredi 14 octobre 2009
Statut
Membre
Dernière intervention
7 mars 2010
-
Messages postés
9
Date d'inscription
mercredi 14 octobre 2009
Statut
Membre
Dernière intervention
7 mars 2010
-
Bonjour à tour, mon code ne fonctionne pas très bien ! Sauriez vous dire pourquoi !

------------fstream.h-----------------------

#ifndef CORE_FSTREAM
#define CORE_FSTREAM

#include "main.h"
#include "sstring.h"
#include "basic_stream.h"

struct _iobuf;
typedef _iobuf FILE;

extern "C++"
{
namespace Sharp
{
class fstream : Stream
{
public:

dll fstream& operator<<(const string&);
dll fstream& operator<<(const int&);
dll fstream& operator<<(const double&);
dll fstream& operator<<(const char&);
dll fstream& operator<<(const unsigned int&);

dll fstream& operator>>(string&);
dll fstream& operator>>(int&);
dll fstream& operator>>(double&);
dll fstream& operator>>(char&);
dll fstream& operator>>(unsigned int&);

dll fstream(string, int = In);
dll fstream();
dll ~fstream();

dll bool open(string, int = In);
dll bool ok();
dll void close();

dll void put(unsigned char);
dll unsigned char get();

dll void puts(string);
dll string gets();

dll void printf(string, ...);
dll void scanf(string, ...);

dll void printlf(string, ...);
dll void scanlf(string, ...);

dll int tell();
dll void seek(int);
dll void rewind();

dll int lenght();
dll bool eof();

dll void del();

private:

dll int flenght();

FILE* stream;

string m_filename;

int m_saveloc;
int m_openmode;
int m_lenght;
bool m_open;
};
};
};

#endif // CORE_FSTREAM

-------------fstream.cpp-----------------

#include "fstream.h"
#include "sstring.h"

#include <cstdio>
#include <stdarg.h>

namespace Sharp
{

dll fstream& fstream::operator<<(const string& s)
{
this->puts(s);

return *this;
}

dll fstream& fstream::operator<<(const int& i)
{
char tmp[30] = "";

::sprintf(tmp, "%d", i);

this->puts(tmp);

return *this;
}

dll fstream& fstream::operator<<(const double& i)
{
char tmp[30] = "";

::sprintf(tmp, "%lf", i);

this->puts(tmp);

return *this;
}

dll fstream& fstream::operator<<(const char& cc)
{
char tmp[1] = "";

tmp[0] = cc;
tmp[1] = '\0';

this->puts(tmp);

return *this;
}

dll fstream& fstream::operator<<(const unsigned int& i)
{
char tmp[30] = "";

::sprintf(tmp, "%u", i);

this->puts(tmp);

return *this;
}

dll fstream& fstream::operator>>(string& s)
{
s = this->gets();

return *this;
}

dll fstream& fstream::operator>>(int& i)
{
::sscanf((string() = this->gets()).c_str(), "%d", &i);

return *this;
}

dll fstream& fstream::operator>>(double& d)
{
::sscanf((string() = this->gets()).c_str(), "%lf", &d);

return *this;
}

dll fstream& fstream::operator>>(unsigned int& ui)
{
::sscanf((string() = this->gets()).c_str(), "%u", &ui);

return *this;
}

dll fstream& fstream::operator>>(char& c)
{
c = this->get();

return *this;
}

dll fstream::fstream(string fname, int fmode)
{
this->open(fname, fmode);
}


dll fstream::fstream()
{
stream = NULL;
m_filename = "";
m_saveloc = 0;
m_lenght = 0;
m_open = false;
}

dll fstream::~fstream()
{}

dll bool fstream::open(string namefile, int a)
{
bool open_ok = false;
m_openmode = a;

FILE* tmp = NULL;

string openmode = "";

switch(a)
{
case In:
openmode = "rb+";
break;

case Out:
openmode = "rb+";
break;

case Append:
openmode = "ab+";
break;

case (In | Out):
openmode = "rb+";
break;

case (In | Out | Trunc):
tmp = fopen(namefile.c_str(), "wb+");
fclose(tmp);
openmode = "rb+";
break;

case (Out | Trunc):
openmode = "wb+";
break;
}

stream = fopen(namefile.c_str(), openmode.c_str());

if (stream != NULL)
{
m_filename = namefile;
m_open = true;
open_ok = true;

m_lenght = flenght();
}

return open_ok;
}

dll void fstream::close()
{
fclose(stream);
m_open = false;
}

dll void fstream::put(unsigned char a)
{
if ((m_openmode & Out) == Out)
{
fputc(a, stream);
m_lenght++;
}
}

dll unsigned char fstream::get()
{
if ((m_openmode & In) == In)
{
return fgetc(stream);
}
else
{
return '\0';
}
}

dll void fstream::puts(string a)
{
if ((m_openmode & Out) == Out)
{
fputs(a.c_str(), stream);
m_lenght += a.size();
}
}

dll string fstream::gets()
{
if ((m_openmode & In) == In)
{
string chain = "";
char got = 0;

char tmp[1];

tmp[1] = '\0';

while ((got != ' ') and (got != '\n') and (got != '\r') and this->eof())
{
got = this->get();

if ((got != ' ') and (got != '\n') and (got != '\r'))
{
tmp[0] = got;

chain += tmp;
}
}

if (got == '\r') this->get();

return chain;
}
else
{
return "/null";
}
}

dll void fstream::scanf(string format, ...)
{
if ((m_openmode & In) == In)
{
va_list arg;
va_start(arg, format);
vfscanf(stream, format.c_str(),arg);
va_end(arg);
}
}

dll void fstream::printf(string format, ...)
{
if ((m_openmode & Out) == Out)
{
va_list arg;
va_start(arg, format);
vfprintf(stream, format.c_str(),arg);
va_end(arg);
}
}

dll void fstream::scanlf(string format, ...)
{
if ((m_openmode & In) == In)
{
va_list arg;
va_start(arg, format);
vfscanf(stream, (format + "\r\n").c_str(), arg);
va_end(arg);
}
}

dll void fstream::printlf(string format, ...)
{
if ((m_openmode & Out) == Out)
{
va_list arg;
va_start(arg, format);
vfprintf(stream, (format + "\r\n").c_str(), arg);
va_end(arg);
}
}

dll int fstream::tell()
{
return (ftell(stream) - 1);
}

dll void fstream::seek(int a)
{
fseek(stream, a, 0);
}

dll void fstream::rewind()
{
this->seek(0);
}

dll int fstream::lenght()
{
return m_lenght;
}

dll bool fstream::ok()
{
return m_open;
}

dll bool fstream::eof()
{
bool f_eof = true;

if (this->tell() >= (this->m_lenght - 1))
f_eof = false;

return f_eof;
}

dll void fstream::del()
{
remove(m_filename.c_str());
}

dll int fstream::flenght()
{
FILE* fic = NULL;

fic = ::fopen(m_filename.c_str(), "rb");

unsigned int nb_char = 0;
int codelu = 0;

while (codelu != (-1))
{
codelu = fgetc(fic);
nb_char++;
}

nb_char--;

::fclose(fic);

return nb_char;
}

};

Note : basic_stream.h contient une une emueration (Stream::In, Stream::Out, Stream::Trunc) et sstring.h contient une classe string infaillible.


Merci de votre aide !

2 réponses

Messages postés
14
Date d'inscription
vendredi 19 novembre 2004
Statut
Membre
Dernière intervention
18 novembre 2009

Bonjour,

Où se situe le problème exactement? Pourquoi ne pas utiliser std::fstream?

jules
Messages postés
9
Date d'inscription
mercredi 14 octobre 2009
Statut
Membre
Dernière intervention
7 mars 2010

Le problème c'est que ce flux personnalisé sert de base à deux ou trois autres modèles plus complexes et comme j'en connait le fonctionnement, ce serait plus facile pour moi de le manipuler !