cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/09/11:59:09

From: "Pieter van Ginkel" <pginkel AT westbrabant DOT net>
To: <djgpp AT delorie DOT com>
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

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 <fstream.h>
#include <routines.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#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<len; i++ )
  {
    file.read( chars, 2 );
    line[i] =3D decryptchar( chars );
    if( line[i]=3D=3Dendchar )
    {
      line[i+1]=3D0;
      break;
    }
  }
  free( chars );
  line[len]=3D0;
}
void ifcrypt::readlen( char * line, int len )
{
  char * chars =3D (char *)malloc( 3 );
  char returned;
  int i;
  for( i=3D0; i<len; i++ )
  {
    file.read( chars, 2 );
    line[i] =3D decryptchar( chars );
  }
  free( chars );
}







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

#include <fstream.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <routines.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#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<len; i++ )
  {
    read( file, chars, 2 );
    line[i] =3D decryptchar( chars );
    if( line[i]=3D=3Dendchar )
    {
      line[i+1]=3D0;
      break;
    }
  }
  line[len]=3D0;
}
void ifcrypt::readlen( char * line, int len )
{
  char chars[2];
  char returned;
  int i;
  for( i=3D0; i<len; i++ )
  {
    read( file, chars, 2 );
    line[i] =3D decryptchar( chars );
  }
}


------=_NextPart_000_000F_01BD3584.8A1614A0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 HTML//EN">
<HTML>
<HEAD>

<META content=3Dtext/html;charset=3Diso-8859-1 =
http-equiv=3DContent-Type>
<META content=3D'"MSHTML 4.71.1712.3"' name=3DGENERATOR>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>I've made a perfectly =
good program,=20
but I have a problem with binary reading of files.<BR>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).<BR>This is the code I've used. Some parts aren't there because of =
security=20
reasons.<BR>By the way, It does work in TC++. I've just transefered my =
whole=20
project to djgpp.<BR>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 =
&quot;in=20
function&quot; that point to I think file routines... The code of that =
(with the=20
::open) is afther the code pointed to abouve.</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>#include=20
&lt;fstream.h&gt;<BR>#include &lt;routines.h&gt;<BR>#include=20
&lt;malloc.h&gt;<BR>#include &lt;stdlib.h&gt;<BR>#include=20
&lt;string.h&gt;</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>#define CRYPT_STRING=20
&quot;...&quot;&nbsp; // the ... because of security.</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>/* The system uses a =
class system=20
similar to the one used by C++. */</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>struct=20
ifcrypt<BR>{<BR>public:<BR>&nbsp; ifcrypt( char * filename, const char * =

_pasword );<BR>&nbsp; ifcrypt( void );<BR>&nbsp; ~ifcrypt( void =
);<BR>&nbsp;=20
void open( char * filename, const char * _pasword );<BR>&nbsp; void =
readline(=20
char * line, int len, char endchar=3D'\n' );<BR>&nbsp; void readlen( =
char * line,=20
int len );<BR>&nbsp; void close( void );</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; bool eof( void=20
)<BR>&nbsp;&nbsp;&nbsp; { return (bool)file.eof(); };<BR>&nbsp; bool =
correct(=20
void )<BR>&nbsp;&nbsp;&nbsp; { return fileopen; };</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>private:<BR>&nbsp; char =
decryptchar(=20
char * chars );</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; char * =
pasword;<BR>&nbsp; bool=20
fileopen;<BR>&nbsp; ifstream file;<BR>};</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>ifcrypt::~ifcrypt( void =

)<BR>{<BR>&nbsp; // This one is emty. Forfuture changes, it is defined=20
anyway.<BR>}</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>char =
ifcrypt::decryptchar( char *=20
chars )<BR>{<BR>&nbsp; // Again security<BR>}</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>ifcrypt::ifcrypt( void=20
)<BR>{<BR>&nbsp; fileopen=3Dfalse;<BR>}</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>ifcrypt::ifcrypt( char =
* filename,=20
const char * _pasword )<BR>{<BR>&nbsp; open( filename, _pasword=20
);<BR>}</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>void ifcrypt::open( =
char * filename,=20
const char * _pasword )<BR>{<BR>&nbsp; char * dummy =3D (char *)malloc( =
strlen(=20
CRYPT_STRING )+1 );</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; file.open( =
filename, 128=20
);</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; pasword =3D =
(char *)malloc(=20
strlen( _pasword )+1 );<BR>&nbsp; strcpy( pasword, _pasword=20
);</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; readline( dummy, =
strlen(=20
CRYPT_STRING ));</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; if( strcmp( =
dummy,=20
CRYPT_STRING )=3D=3D0 )<BR>&nbsp;&nbsp;&nbsp; fileopen=3Dtrue;<BR>&nbsp; =

else<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; =
fileopen=3Dfalse;<BR>&nbsp;&nbsp;&nbsp;=20
file.close();<BR>&nbsp; }</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; free( dummy =
);</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp;=20
return;<BR>}</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>void ifcrypt::close( =
void=20
)<BR>{<BR>&nbsp; file.close();<BR>&nbsp; free( pasword=20
);<BR>}</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>void ifcrypt::readline( =
char * line,=20
int len, char endchar )<BR>{<BR>&nbsp; char * chars =3D (char *)malloc( =
3=20
);<BR>&nbsp; char returned;</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; int =
i;</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; for( i=3D0; =
i&lt;len; i++=20
)<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; file.read( chars, 2 =
);<BR>&nbsp;&nbsp;&nbsp;=20
line[i] =3D decryptchar( chars );</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; if(=20
line[i]=3D=3Dendchar )<BR>&nbsp;&nbsp;&nbsp; =
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
line[i+1]=3D0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
break;<BR>&nbsp;&nbsp;&nbsp;=20
}<BR>&nbsp; }</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; free( chars =
);</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp;=20
line[len]=3D0;<BR>}</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>void ifcrypt::readlen( =
char * line,=20
int len )<BR>{<BR>&nbsp; char * chars =3D (char *)malloc( 3 );<BR>&nbsp; =
char=20
returned;</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; int =
i;</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; for( i=3D0; =
i&lt;len; i++=20
)<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; file.read( chars, 2 =
);<BR>&nbsp;&nbsp;&nbsp;=20
line[i] =3D decryptchar( chars );<BR>&nbsp; }</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; free( chars=20
);<BR>}</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2><BR>// And the second =
part, with=20
::open...</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2><BR>#include=20
&lt;fstream.h&gt;<BR>#include &lt;fcntl.h&gt;<BR>#include=20
&lt;unistd.h&gt;<BR>#include &lt;sys/stat.h&gt;<BR>#include=20
&lt;routines.h&gt;<BR>#include &lt;malloc.h&gt;<BR>#include=20
&lt;stdlib.h&gt;<BR>#include &lt;string.h&gt;</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>#define CRYPT_STRING=20
&quot;...&quot;&nbsp; // Yes, security</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2><BR>/* The system uses =
a class system=20
similar to the one used by C++. */</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>struct=20
ifcrypt<BR>{<BR>public:<BR>&nbsp; ifcrypt( char * filename, const char * =

_pasword );<BR>&nbsp; ifcrypt( void );<BR>&nbsp; ~ifcrypt( void =
);<BR>&nbsp;=20
void open( char * filename, const char * _pasword );<BR>&nbsp; void =
readline(=20
char * line, int len, char endchar=3D'\n' );<BR>&nbsp; void readlen( =
char * line,=20
int len );<BR>&nbsp; void close( void );</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; bool correct( =
void=20
)<BR>&nbsp;&nbsp;&nbsp; { return !file&lt;0; };</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>private:<BR>&nbsp; char =
decryptchar(=20
char * chars );</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; char * =
pasword;<BR>&nbsp; bool=20
fileopen;<BR>&nbsp; int file;<BR>};</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>ifcrypt::~ifcrypt( void =

)<BR>{<BR>&nbsp; // This one is emty. Forfuture changes, it is defined=20
anyway.<BR>}</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>char =
ifcrypt::decryptchar( char *=20
chars )<BR>{<BR>&nbsp; // Yes, yes, yes<BR>}</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>ifcrypt::ifcrypt( void=20
)<BR>{<BR>&nbsp; fileopen=3Dfalse;<BR>}</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>ifcrypt::ifcrypt( char =
* filename,=20
const char * _pasword )<BR>{<BR>&nbsp; open( filename, _pasword=20
);<BR>}</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>void ifcrypt::open( =
char * filename,=20
const char * _pasword )<BR>{<BR>&nbsp; char * dummy =3D (char *)malloc( =
strlen(=20
CRYPT_STRING )+1 );</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; file =3D ::open( =
filename,=20
O_BINARY|O_RDONLY );</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; pasword =3D =
(char *)malloc(=20
strlen( _pasword )+1 );<BR>&nbsp; strcpy( pasword, _pasword=20
);</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; readline( dummy, =
strlen(=20
CRYPT_STRING ));</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; if( strcmp( =
dummy,=20
CRYPT_STRING )=3D=3D0 )<BR>&nbsp;&nbsp;&nbsp; fileopen=3Dtrue;<BR>&nbsp; =

else<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; =
fileopen=3Dfalse;<BR>&nbsp;&nbsp;&nbsp;=20
::close( file );<BR>&nbsp; }</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; free( dummy =
);</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp;=20
return;<BR>}</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>void ifcrypt::close( =
void=20
)<BR>{<BR>&nbsp; ::close( file );<BR>&nbsp; free( pasword=20
);<BR>}</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>void ifcrypt::readline( =
char * line,=20
int len, char endchar )<BR>{<BR>&nbsp; char chars[2];<BR>&nbsp; char=20
returned;</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; int =
i;</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; for( i=3D0; =
i&lt;len; i++=20
)<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; read( file, chars, 2=20
);<BR>&nbsp;&nbsp;&nbsp; line[i] =3D decryptchar( chars =
);</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; if(=20
line[i]=3D=3Dendchar )<BR>&nbsp;&nbsp;&nbsp; =
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
line[i+1]=3D0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
break;<BR>&nbsp;&nbsp;&nbsp;=20
}<BR>&nbsp; }</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp;=20
line[len]=3D0;<BR>}</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>void ifcrypt::readlen( =
char * line,=20
int len )<BR>{<BR>&nbsp; char chars[2];<BR>&nbsp; char=20
returned;</FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; int =
i;</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>&nbsp; for( i=3D0; =
i&lt;len; i++=20
)<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; read( file, chars, 2=20
);<BR>&nbsp;&nbsp;&nbsp; line[i] =3D decryptchar( chars );<BR>&nbsp;=20
}<BR>}<BR></FONT>&nbsp;</DIV></BODY></HTML>

------=_NextPart_000_000F_01BD3584.8A1614A0--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019