Date: Tue, 12 Dec 95 14:39:30 EST From: Brian_Pence AT aeronomics DOT com To: djgpp AT sun DOT soe DOT clarkson DOT edu, cbutler AT bnr DOT ca (Chris Butler) Subject: Re: Strange bug using 'fwrite' fwrite is sending 0D (hex) 0A (hex) for every 0A I believe in UNIX, a 0A is a NEWLINE, and it translates the NEWLINE into a Carriage Return & Linefeed pair. The same thing seems to be happening here. Open the file with "wb" (write binary) instead of "w". ______________________________ Reply Separator _________________________________ Subject: Strange bug using 'fwrite' Author: cbutler%bnr DOT ca AT aerohpux (Chris Butler) at UnixGateway Date: 12/12/95 12:45 PM Hi folks, I was coding away this weekend when the following bug stopped me dead. I'm using fwrite to write bytes to a file. Here's a program sample: -------------------------------- #include #include #include main() { FILE *f2; char vers_update[5]; int rec_count = 1; short hlength = 2, reclength = 3; char extra_bytes[25]; int i; for (i = 0; i < 20; i++) extra_bytes[i] = i + 5; strcpy(vers_update, "abcd"); f2 = fopen("out.2", "w"); if (f2 == 0) { printf("Couldn't open '%s' for writing... aborting.\n", "out.2"); exit(1); } fwrite(vers_update, 1, 4, f2); fwrite(&rec_count, 1, 4, f2); fwrite(&hlength, 1, 2, f2); fwrite(&reclength, 1, 2, f2); fwrite(extra_bytes, 1, 20, f2); fclose(f2); } ---------------------------- Please, no smart remarks about coding style - I cut & pasted this together. Anyway, when I compile and run this on my PC, I get the following hex dump from the file "out.2": 61 62 63 64 01 00 00 00 02 00 03 00 05 06 07 08 09 0d 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 ....for a grand total of 33 bytes, where I was only expecting 32. Note also the '0d', second byte on the 2nd line. Anyone have any ideas where it came from? The program works as I'd expect it to on UNIX. I haven't tried rewriting the above to use file descriptors instead of file pointers, so I'm not sure what happens there. Also, I messed around with the initialization of the 'extra_bytes' in other tests - the '0d' always seems to follow the '09'. System info: 486 80 MHz CPU, 4 megs RAM, gcc v2.6.3 . Any info would be appreciated - thanks. -Chris -- Chris Butler aka cbutler AT bnr DOT ca -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- All standard disclaimers apply. Have an adequate day.