From: kagel AT quasar DOT bloomberg DOT com Date: Fri, 1 Nov 1996 07:41:25 -0500 Message-Id: <9611011241.AA06279@quasar.bloomberg.com > To: indrek AT warp DOT edu DOT ee Cc: djgpp AT delorie DOT com In-Reply-To: (message from Indrek Mandre on Fri, 1 Nov 1996 00:54:40 +0200 (EET)) Subject: Re: Problems with fread/fwrite Reply-To: kagel AT dg1 DOT bloomberg DOT com Errors-To: postmaster AT bloomberg DOT com Date: Fri, 1 Nov 1996 00:54:40 +0200 (EET) From: Indrek Mandre Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Content-Length: 1692 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 #include 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 */ Looks like you need to open the file in binary mode. Under LINUX and other UNIXs it does not matter but under MSDOS character translation happens in text mode files. Make the fopens like so: fl = fopen ( "kace.aaa", "wb" ); /* Lets write it to file */ fl = fopen ( "kace.aaa", "rb" ); /* Lets read it */ This is ANSI "C" and acceptable under LINUX though GCC just ignores it there. To write portable code you must remember the binary mode flag on all binary fopens or the O_BINARY mode bit in open() & creat(). 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 #include 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 -- Art S. Kagel, kagel AT quasar DOT bloomberg DOT com A proverb is no proverb to you 'till life has illustrated it. -- John Keats