From: "Pieter van Ginkel" To: Subject: Fstream, binary, ::open and sigabrt. Arggggggggggggg Date: Mon, 9 Feb 1998 18:00:00 +0100 Message-ID: <01bd357c$2851aca0$cf2f86c2@pieter> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_000F_01BD3584.8A1614A0" Precedence: bulk Dit is een meerdelig bericht in MIME-indeling. ------=_NextPart_000_000F_01BD3584.8A1614A0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable I've made a perfectly good program, but I have a problem with binary = reading of files. I use ifstream to gain access to a file, but fstream doesn't want to = accept my ios::binary acces identifier. I know this because if i use = just open( bla, O_BINARY ), it opens the file as it should, but if I = open it with fstream, it is open (eof) before I begin reading and closed = when I've read my first few characters (again with eof). This is the code I've used. Some parts aren't there because of security = reasons. By the way, It does work in TC++. I've just transefered my whole project = to djgpp. And another thing. If I use just open and close and read etc., I get a = SIGABRT signal when I trie to access file.close and after that ::close. = It gives the stack, and it points to first the line of the close and = second the end of the file. After that there comes a few whings like "in = function" that point to I think file routines... The code of that (with = the ::open) is afther the code pointed to abouve. #include #include #include #include #include #define CRYPT_STRING "..." // the ... because of security. /* The system uses a class system similar to the one used by C++. */ struct ifcrypt { public: ifcrypt( char * filename, const char * _pasword ); ifcrypt( void ); ~ifcrypt( void ); void open( char * filename, const char * _pasword ); void readline( char * line, int len, char endchar=3D'\n' ); void readlen( char * line, int len ); void close( void ); bool eof( void ) { return (bool)file.eof(); }; bool correct( void ) { return fileopen; }; private: char decryptchar( char * chars ); char * pasword; bool fileopen; ifstream file; }; ifcrypt::~ifcrypt( void ) { // This one is emty. Forfuture changes, it is defined anyway. } char ifcrypt::decryptchar( char * chars ) { // Again security } ifcrypt::ifcrypt( void ) { fileopen=3Dfalse; } ifcrypt::ifcrypt( char * filename, const char * _pasword ) { open( filename, _pasword ); } void ifcrypt::open( char * filename, const char * _pasword ) { char * dummy =3D (char *)malloc( strlen( CRYPT_STRING )+1 ); file.open( filename, 128 ); pasword =3D (char *)malloc( strlen( _pasword )+1 ); strcpy( pasword, _pasword ); readline( dummy, strlen( CRYPT_STRING )); if( strcmp( dummy, CRYPT_STRING )=3D=3D0 ) fileopen=3Dtrue; else { fileopen=3Dfalse; file.close(); } free( dummy ); return; } void ifcrypt::close( void ) { file.close(); free( pasword ); } void ifcrypt::readline( char * line, int len, char endchar ) { char * chars =3D (char *)malloc( 3 ); char returned; int i; for( i=3D0; i #include #include #include #include #include #include #include #define CRYPT_STRING "..." // Yes, security /* The system uses a class system similar to the one used by C++. */ struct ifcrypt { public: ifcrypt( char * filename, const char * _pasword ); ifcrypt( void ); ~ifcrypt( void ); void open( char * filename, const char * _pasword ); void readline( char * line, int len, char endchar=3D'\n' ); void readlen( char * line, int len ); void close( void ); bool correct( void ) { return !file<0; }; private: char decryptchar( char * chars ); char * pasword; bool fileopen; int file; }; ifcrypt::~ifcrypt( void ) { // This one is emty. Forfuture changes, it is defined anyway. } char ifcrypt::decryptchar( char * chars ) { // Yes, yes, yes } ifcrypt::ifcrypt( void ) { fileopen=3Dfalse; } ifcrypt::ifcrypt( char * filename, const char * _pasword ) { open( filename, _pasword ); } void ifcrypt::open( char * filename, const char * _pasword ) { char * dummy =3D (char *)malloc( strlen( CRYPT_STRING )+1 ); file =3D ::open( filename, O_BINARY|O_RDONLY ); pasword =3D (char *)malloc( strlen( _pasword )+1 ); strcpy( pasword, _pasword ); readline( dummy, strlen( CRYPT_STRING )); if( strcmp( dummy, CRYPT_STRING )=3D=3D0 ) fileopen=3Dtrue; else { fileopen=3Dfalse; ::close( file ); } free( dummy ); return; } void ifcrypt::close( void ) { ::close( file ); free( pasword ); } void ifcrypt::readline( char * line, int len, char endchar ) { char chars[2]; char returned; int i; for( i=3D0; i
I've made a perfectly = good program,=20 but I have a problem with binary reading of files.
I use ifstream to = gain=20 access to a file, but fstream doesn't want to accept my ios::binary = acces=20 identifier. I know this because if i use just open( bla, O_BINARY ), it = opens=20 the file as it should, but if I open it with fstream, it is open (eof) = before I=20 begin reading and closed when I've read my first few characters (again = with=20 eof).
This is the code I've used. Some parts aren't there because of = security=20 reasons.
By the way, It does work in TC++. I've just transefered my = whole=20 project to djgpp.
And another thing. If I use just open and close and = read=20 etc., I get a SIGABRT signal when I trie to access file.close and after = that=20 ::close. It gives the stack, and it points to first the line of the = close and=20 second the end of the file. After that there comes a few whings like = "in=20 function" that point to I think file routines... The code of that = (with the=20 ::open) is afther the code pointed to abouve.
 
 
 
 
#include=20 <fstream.h>
#include <routines.h>
#include=20 <malloc.h>
#include <stdlib.h>
#include=20 <string.h>
 
#define CRYPT_STRING=20 "..."  // the ... because of security.
/* The system uses a = class system=20 similar to the one used by C++. */
struct=20 ifcrypt
{
public:
  ifcrypt( char * filename, const char * = _pasword );
  ifcrypt( void );
  ~ifcrypt( void = );
 =20 void open( char * filename, const char * _pasword );
  void = readline(=20 char * line, int len, char endchar=3D'\n' );
  void readlen( = char * line,=20 int len );
  void close( void );
 
  bool eof( void=20 )
    { return (bool)file.eof(); };
  bool = correct(=20 void )
    { return fileopen; };
 
private:
  char = decryptchar(=20 char * chars );
 
  char * = pasword;
  bool=20 fileopen;
  ifstream file;
};
 
ifcrypt::~ifcrypt( void = )
{
  // This one is emty. Forfuture changes, it is defined=20 anyway.
}
 
char = ifcrypt::decryptchar( char *=20 chars )
{
  // Again security
}
 
ifcrypt::ifcrypt( void=20 )
{
  fileopen=3Dfalse;
}
 
ifcrypt::ifcrypt( char = * filename,=20 const char * _pasword )
{
  open( filename, _pasword=20 );
}
 
void ifcrypt::open( = char * filename,=20 const char * _pasword )
{
  char * dummy =3D (char *)malloc( = strlen(=20 CRYPT_STRING )+1 );
 
  file.open( = filename, 128=20 );
  pasword =3D = (char *)malloc(=20 strlen( _pasword )+1 );
  strcpy( pasword, _pasword=20 );
 
  readline( dummy, = strlen(=20 CRYPT_STRING ));
  if( strcmp( = dummy,=20 CRYPT_STRING )=3D=3D0 )
    fileopen=3Dtrue;
  = else
  {
    = fileopen=3Dfalse;
   =20 file.close();
  }
 
  free( dummy = );
 =20 return;
}
 
void ifcrypt::close( = void=20 )
{
  file.close();
  free( pasword=20 );
}
 
void ifcrypt::readline( = char * line,=20 int len, char endchar )
{
  char * chars =3D (char *)malloc( = 3=20 );
  char returned;
 
  int = i;
  for( i=3D0; = i<len; i++=20 )
  {
    file.read( chars, 2 = );
   =20 line[i] =3D decryptchar( chars );
 
    if(=20 line[i]=3D=3Dendchar )
    = {
     =20 line[i+1]=3D0;
      = break;
   =20 }
  }
 
  free( chars = );
 =20 line[len]=3D0;
}
 
void ifcrypt::readlen( = char * line,=20 int len )
{
  char * chars =3D (char *)malloc( 3 );
  = char=20 returned;
 
  int = i;
  for( i=3D0; = i<len; i++=20 )
  {
    file.read( chars, 2 = );
   =20 line[i] =3D decryptchar( chars );
  }
 
  free( chars=20 );
}
 
 
 
 
 
 
 

// And the second = part, with=20 ::open...
 

#include=20 <fstream.h>
#include <fcntl.h>
#include=20 <unistd.h>
#include <sys/stat.h>
#include=20 <routines.h>
#include <malloc.h>
#include=20 <stdlib.h>
#include <string.h>
 
#define CRYPT_STRING=20 "..."  // Yes, security

/* The system uses = a class system=20 similar to the one used by C++. */
 
struct=20 ifcrypt
{
public:
  ifcrypt( char * filename, const char * = _pasword );
  ifcrypt( void );
  ~ifcrypt( void = );
 =20 void open( char * filename, const char * _pasword );
  void = readline(=20 char * line, int len, char endchar=3D'\n' );
  void readlen( = char * line,=20 int len );
  void close( void );
 
  bool correct( = void=20 )
    { return !file<0; };
 
private:
  char = decryptchar(=20 char * chars );
 
  char * = pasword;
  bool=20 fileopen;
  int file;
};
 
ifcrypt::~ifcrypt( void = )
{
  // This one is emty. Forfuture changes, it is defined=20 anyway.
}
 
char = ifcrypt::decryptchar( char *=20 chars )
{
  // Yes, yes, yes
}
 
ifcrypt::ifcrypt( void=20 )
{
  fileopen=3Dfalse;
}
 
ifcrypt::ifcrypt( char = * filename,=20 const char * _pasword )
{
  open( filename, _pasword=20 );
}
 
void ifcrypt::open( = char * filename,=20 const char * _pasword )
{
  char * dummy =3D (char *)malloc( = strlen(=20 CRYPT_STRING )+1 );
 
  file =3D ::open( = filename,=20 O_BINARY|O_RDONLY );
  pasword =3D = (char *)malloc(=20 strlen( _pasword )+1 );
  strcpy( pasword, _pasword=20 );
 
  readline( dummy, = strlen(=20 CRYPT_STRING ));
  if( strcmp( = dummy,=20 CRYPT_STRING )=3D=3D0 )
    fileopen=3Dtrue;
  = else
  {
    = fileopen=3Dfalse;
   =20 ::close( file );
  }
 
  free( dummy = );
 =20 return;
}
 
void ifcrypt::close( = void=20 )
{
  ::close( file );
  free( pasword=20 );
}
 
void ifcrypt::readline( = char * line,=20 int len, char endchar )
{
  char chars[2];
  char=20 returned;
 
  int = i;
  for( i=3D0; = i<len; i++=20 )
  {
    read( file, chars, 2=20 );
    line[i] =3D decryptchar( chars = );
 
    if(=20 line[i]=3D=3Dendchar )
    = {
     =20 line[i+1]=3D0;
      = break;
   =20 }
  }
 
 =20 line[len]=3D0;
}
 
void ifcrypt::readlen( = char * line,=20 int len )
{
  char chars[2];
  char=20 returned;
 
  int = i;
  for( i=3D0; = i<len; i++=20 )
  {
    read( file, chars, 2=20 );
    line[i] =3D decryptchar( chars );
 =20 }
}
 
------=_NextPart_000_000F_01BD3584.8A1614A0--