delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/10/31/18:12:55

Date: Fri, 1 Nov 1996 00:54:40 +0200 (EET)
From: Indrek Mandre <indrek AT warp DOT edu DOT ee>
To: djgpp AT delorie DOT com
Subject: Problems with fread/fwrite
Message-ID: <Pine.LNX.3.91.961101004806.67A-100000@warp.edu.ee>
MIME-Version: 1.0

Hello! I'v ran in trouble again!
My stuff from Linux wont work under DJGPP2.
I think I'v found a bug in fread, or in myself, who knows...
Example:

#include <stdio.h>
#include <stdlib.h>

struct proov {				/* Little nice structure */
	int mass[255];
	int	z;
};

void main()
{
struct proov *p, *t;
FILE *fl;

 p = malloc ( sizeof ( struct proov ) );    /* Lets allocate memory */
 t = malloc ( sizeof ( struct proov ) );

 fl = fopen ( "kace.aaa", "w" );	    /* Lets write it to file */
	p->z = 1999;			    /* Important ZZzzzz... */
	fwrite ( p, sizeof ( struct proov ), 1, fl );
 fclose (fl );

 fl = fopen ( "kace.aaa", "r" );	    /* Lets read it */
	fread ( t, sizeof ( struct proov ), 1, fl );
 fclose (fl);

 printf("%d\n", t->z );			    /* It isn't 1999 */
					    /* 1/15 is 1999  */
}

 On my machine t->z isn't at the end 1999! But on my Linux it
works perfectly! Where is the problem? In my machine? Or should
I use only open and read and write and will they work?
I ran it in pure DOS box.

Another problem with fread/fwrite:

#include <stdio.h>
#include <stdlib.h>

void main()
{
 FILE *fl;
 char a[22000];
 char b[2000];

 fl = fopen ( "kace.aaa", "w" );
	fwrite ( a, 22000, 1, fl );	/* Created a file - 22000 bytes */
 fclose ( fl );				/* It's never so small - why?   */

 fl = fopen ( "kace.aaa", "r" );
	printf("%d\n", fread ( b, 1, 1000, fl ));  /* Says here random  */
	printf("%d\n", fread ( a, 1, 21000, fl )); /* Says here random  */
 fclose (fl);

}

Again on Linux works (gcc 2.7.0).

Does fread/fwrite work? It isn't in my box :(
Everything else seems to work perfectly, of course before I test it :)

Indrek Mandre
indrek AT warp DOT edu DOT ee
http://www.ttg.edu.ee/~indrek

- Raw text -


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