Mail Archives: djgpp/1998/02/09/11:59:09
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 =
"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.</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2></FONT> </DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>#include=20
<fstream.h><BR>#include <routines.h><BR>#include=20
<malloc.h><BR>#include <stdlib.h><BR>#include=20
<string.h></FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>#define CRYPT_STRING=20
"..." // 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> ifcrypt( char * filename, const char * =
_pasword );<BR> ifcrypt( void );<BR> ~ifcrypt( void =
);<BR> =20
void open( char * filename, const char * _pasword );<BR> void =
readline(=20
char * line, int len, char endchar=3D'\n' );<BR> void readlen( =
char * line,=20
int len );<BR> void close( void );</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> bool eof( void=20
)<BR> { return (bool)file.eof(); };<BR> bool =
correct(=20
void )<BR> { return fileopen; };</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>private:<BR> char =
decryptchar(=20
char * chars );</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> char * =
pasword;<BR> bool=20
fileopen;<BR> ifstream file;<BR>};</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>ifcrypt::~ifcrypt( void =
)<BR>{<BR> // This one is emty. Forfuture changes, it is defined=20
anyway.<BR>}</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>char =
ifcrypt::decryptchar( char *=20
chars )<BR>{<BR> // Again security<BR>}</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>ifcrypt::ifcrypt( void=20
)<BR>{<BR> fileopen=3Dfalse;<BR>}</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>ifcrypt::ifcrypt( char =
* filename,=20
const char * _pasword )<BR>{<BR> open( filename, _pasword=20
);<BR>}</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>void ifcrypt::open( =
char * filename,=20
const char * _pasword )<BR>{<BR> char * dummy =3D (char *)malloc( =
strlen(=20
CRYPT_STRING )+1 );</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> file.open( =
filename, 128=20
);</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> pasword =3D =
(char *)malloc(=20
strlen( _pasword )+1 );<BR> strcpy( pasword, _pasword=20
);</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> readline( dummy, =
strlen(=20
CRYPT_STRING ));</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> if( strcmp( =
dummy,=20
CRYPT_STRING )=3D=3D0 )<BR> fileopen=3Dtrue;<BR> =
else<BR> {<BR> =
fileopen=3Dfalse;<BR> =20
file.close();<BR> }</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> free( dummy =
);</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> =20
return;<BR>}</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>void ifcrypt::close( =
void=20
)<BR>{<BR> file.close();<BR> free( pasword=20
);<BR>}</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>void ifcrypt::readline( =
char * line,=20
int len, char endchar )<BR>{<BR> char * chars =3D (char *)malloc( =
3=20
);<BR> char returned;</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> int =
i;</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> for( i=3D0; =
i<len; i++=20
)<BR> {<BR> file.read( chars, 2 =
);<BR> =20
line[i] =3D decryptchar( chars );</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> if(=20
line[i]=3D=3Dendchar )<BR> =
{<BR> =20
line[i+1]=3D0;<BR> =
break;<BR> =20
}<BR> }</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> free( chars =
);</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> =20
line[len]=3D0;<BR>}</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>void ifcrypt::readlen( =
char * line,=20
int len )<BR>{<BR> char * chars =3D (char *)malloc( 3 );<BR> =
char=20
returned;</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> int =
i;</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> for( i=3D0; =
i<len; i++=20
)<BR> {<BR> file.read( chars, 2 =
);<BR> =20
line[i] =3D decryptchar( chars );<BR> }</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> free( chars=20
);<BR>}</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2><BR>// And the second =
part, with=20
::open...</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2><BR>#include=20
<fstream.h><BR>#include <fcntl.h><BR>#include=20
<unistd.h><BR>#include <sys/stat.h><BR>#include=20
<routines.h><BR>#include <malloc.h><BR>#include=20
<stdlib.h><BR>#include <string.h></FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>#define CRYPT_STRING=20
"..." // 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> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>struct=20
ifcrypt<BR>{<BR>public:<BR> ifcrypt( char * filename, const char * =
_pasword );<BR> ifcrypt( void );<BR> ~ifcrypt( void =
);<BR> =20
void open( char * filename, const char * _pasword );<BR> void =
readline(=20
char * line, int len, char endchar=3D'\n' );<BR> void readlen( =
char * line,=20
int len );<BR> void close( void );</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> bool correct( =
void=20
)<BR> { return !file<0; };</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>private:<BR> char =
decryptchar(=20
char * chars );</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> char * =
pasword;<BR> bool=20
fileopen;<BR> int file;<BR>};</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>ifcrypt::~ifcrypt( void =
)<BR>{<BR> // This one is emty. Forfuture changes, it is defined=20
anyway.<BR>}</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>char =
ifcrypt::decryptchar( char *=20
chars )<BR>{<BR> // Yes, yes, yes<BR>}</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>ifcrypt::ifcrypt( void=20
)<BR>{<BR> fileopen=3Dfalse;<BR>}</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>ifcrypt::ifcrypt( char =
* filename,=20
const char * _pasword )<BR>{<BR> open( filename, _pasword=20
);<BR>}</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>void ifcrypt::open( =
char * filename,=20
const char * _pasword )<BR>{<BR> char * dummy =3D (char *)malloc( =
strlen(=20
CRYPT_STRING )+1 );</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> file =3D ::open( =
filename,=20
O_BINARY|O_RDONLY );</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> pasword =3D =
(char *)malloc(=20
strlen( _pasword )+1 );<BR> strcpy( pasword, _pasword=20
);</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> readline( dummy, =
strlen(=20
CRYPT_STRING ));</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> if( strcmp( =
dummy,=20
CRYPT_STRING )=3D=3D0 )<BR> fileopen=3Dtrue;<BR> =
else<BR> {<BR> =
fileopen=3Dfalse;<BR> =20
::close( file );<BR> }</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> free( dummy =
);</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> =20
return;<BR>}</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>void ifcrypt::close( =
void=20
)<BR>{<BR> ::close( file );<BR> free( pasword=20
);<BR>}</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>void ifcrypt::readline( =
char * line,=20
int len, char endchar )<BR>{<BR> char chars[2];<BR> char=20
returned;</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> int =
i;</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> for( i=3D0; =
i<len; i++=20
)<BR> {<BR> read( file, chars, 2=20
);<BR> line[i] =3D decryptchar( chars =
);</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> if(=20
line[i]=3D=3Dendchar )<BR> =
{<BR> =20
line[i+1]=3D0;<BR> =
break;<BR> =20
}<BR> }</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> =20
line[len]=3D0;<BR>}</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2>void ifcrypt::readlen( =
char * line,=20
int len )<BR>{<BR> char chars[2];<BR> char=20
returned;</FONT> </DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> int =
i;</FONT></DIV>
<DIV><FONT color=3D#000000 face=3DArial size=3D2> for( i=3D0; =
i<len; i++=20
)<BR> {<BR> read( file, chars, 2=20
);<BR> line[i] =3D decryptchar( chars );<BR> =20
}<BR>}<BR></FONT> </DIV></BODY></HTML>
------=_NextPart_000_000F_01BD3584.8A1614A0--
- Raw text -