www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/11/01/14:28:11

Date: Fri, 1 Nov 1996 21:06:13 +0200 (EET)
From: Indrek Mandre <indrek AT warp DOT edu DOT ee>
To: "John M. Aldrich" <fighteer AT cs DOT com>
cc: djgpp AT delorie DOT com
Subject: Re: Problems with fread/fwrite
In-Reply-To: <327989E8.170C@cs.com>
Message-ID: <Pine.LNX.3.91.961101194440.220A-100000@warp.edu.ee>
MIME-Version: 1.0

On Thu, 31 Oct 1996, John M. Aldrich wrote:
> 
> The bugs are not in DJGPP, but rather in some wrong assumptions you are
> making about the way memory and file handling is done in C.

Yes, I'v never programmed on DOS, but I have correct assumptions.

> 
> Example 1:
>   You should be opening the file in binary mode, not text mode (with "wb"/"rb"
>   instead of "w"/"r").  Why?  Because DOS text uses a CRLF combination to
>   denote end of line, while Unix/Linux only uses a CR.  You should never
>   use fread()/fwrite() in conjunction with files in text mode.

Well, I have only 1 thing to say and think: DOS _is_ stupid.
I thougt that djgpp is like gcc. But now I know - djgpp is not gcc.
Djgpp is stupid????

> Ex. 2
> The reason this problem is being triggered for you is because you are never 
> initializing any of the variables you declare.  The ANSI spec makes no
> guarantee that malloc'ed memory is zeroed, nor non-static local variables,

Wrong! It's only because of the fopen/fread.
I'm not using ANSI C. I think djgpp is _not_ ansi c. I'v reed only one
book - Kerninghan-Ritchie's "The C programming language". My programming
is based on that. There was no need to zero variables.

> Because you never initialize them, p, t, a, and b all contain garbage.  Some
> of this garbage likely has CR and EOF characters in it, which are passed
> verbatim in binary mode, but are handled completely differently in text mode.
> If EOF is present in any of that garbage, the text mode commands will think
> that this denotes the end of the file, and stop reading at that point.

The idea was not to set any other "aaaaaaaaaaaa..." in these variables.
I'm not so stupid. What are these text mode commands?

> 
> Try running under a debugger (gdb, edebug32, fsdb) and examine the contents
> of those variables after you declare them.  You'll see what I mean.

Well, I was born using debugger :)

>
> Below I have attached fixed versions of your programs (note some other
> things corrected as well).  Try these and see if they work; they do for me.

Thank you, I have to say it - you are so smart!

> int main( void )			/* THIS IS THE CORRECT WAY TO DECLARE MAIN */
  
No, void main ( void ) is! I'm right. You are right. In gcc there is no
difference, or is there? Explain me please. Nowhere is said what is correct. 

> {
> static struct proov proov_zero;   	/* this will be initialized to zero because it's static */
> struct proov *p, *t;
> FILE *fl;
> 
>  p = malloc ( sizeof ( struct proov ) );    /* Let's allocate memory */
>  t = malloc ( sizeof ( struct proov ) );
> 
>  *p = proov_zero;	/* copy zeroed structure */
>  *t = proov_zero;

Here I describe my understanding of stru. variables: I think * structures
are like memory areas. Only I have declared to C that they consist of
elements - int, short, char, float[256], ...  - 4, 2, 1, ... bytes in 
specific order. You copy the structures - why to waste memory? Better:
	memset ( p, 0, sizeof (struct proov ) );
If you don't like memset, use bzero. Do I have to say to memset that
it must work in binary mode? I guess not!

> 
>  char a[22000] = "";		/* define and initialize to all zeroes */
>  char b[2000] = "";

All that zeroing is never needed. In my programs I use aaa[5000000] -
every time to zero it?, why to waste time...

Well. Is out there anyone who is using fread/fwrite in not bin mode.
I think nobody. So why is fread so stupid? Is it possible to make
fread work in any mode? Another question: does gets/scanf/??? work
in bin mode?
Is there any other stupid problems I may rendezvous?

Indrek Mandre
indrek AT warp DOT edu DOT ee

- Raw text -


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