X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f From: Radical NetSurfer Newsgroups: comp.os.msdos.djgpp Subject: MAJOR PAIN: Reading Integers! Date: Sat, 25 Dec 2004 02:11:42 -0500 Message-ID: X-Newsreader: Forte Agent 1.93/32.576 English (American) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Organization: Velocity.Net Cache-Post-Path: web.velocity.net!unknown AT 66-211-206-248 DOT velocity DOT net X-Cache: nntpcache 3.0.1 (see http://www.nntpcache.org/) Lines: 126 X-Complaints-To: abuse AT newshosting DOT com To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Here's a little something I hope triggers some discussion... DJGPP has *MANY* features simply not found in most compilers, Borland, visualc, etc... but even DJGPP still requires a bit of thoughtful ingenuity to get access to some values in a file.... Case in point is reading of a "Raw Binary Value" directly out of a file. By "Raw Binary Value" we mean *NOT* being switched around by Intel's storage format (big vs little endian). 1. Lets create a binary file, containing an unsigned long: outfile = fopen ("bf.bin", "wb"); fprintf(outfile,"\x12\x34\x56\x78"); fclose(outfile); //4-bytes expected and NOTHING else. 2. Object here is to now obtain in an unsigned long, with as little effort as possible the ORIGINAL ALIGNMNET of BYTES (defeating intel format); what I am calling "raw binary value" 3. Attemp #1: struct lbf { unsigned long bf:32; } abf; void main(void) { FILE *infile; FILE *outfile; outfile = fopen ("bf.bin", "wb"); fprintf(outfile,"\x12\x34\x56\x78"); fclose(outfile); infile = fopen ("bf.bin", "rb"); fread(&abf, sizeof(abf), 1, infile); fclose(infile); printf("bf = %lu = %X\n", abf.bf, abf.bf ); /* Guess what! Its *NOT* a raw bitfield!! It's actually in Intels format! */ }; 4. Attempt #2: unsigned long abf; unsigned char Buf[4]; void main(void) { FILE *infile; FILE *outfile; outfile = fopen ("bf.bin", "wb"); fprintf(outfile,"\x12\x34\x56\x78"); fclose(outfile); infile = fopen ("bf.bin", "rb"); fread(Buf, sizeof(Buf), 1, infile); fclose(infile); abf = (unsigned long)((Buf[0] << 24) | (Buf[1] << 16) | (Buf[2] << 8) | Buf[3]); printf("bf = %lu = %X\n", abf.bf, abf.bf ); /* Works at the expense of Shifts & Ors, Casting required. */ } //main WHICH ENDS up being resolved into: 5. Attempt #3: unsigned long abf; unsigned char Buf[4]; void main(void) { FILE *infile; FILE *outfile; outfile = fopen ("bf.bin", "wb"); fprintf(outfile,"\x12\x34\x56\x78"); fclose(outfile); infile = fopen ("bf.bin", "rb"); fread(Buf[3], sizeof(char), 1, infile); fread(Buf[2], sizeof(char), 1, infile); fread(Buf[1], sizeof(char), 1, infile); fread(Buf[0], sizeof(char), 1, infile); fclose(infile); abf = cvul( Buf ); //Expect Unsigned Char Address Space to be UInt printf("bf = %lu = %X\n", abf.bf, abf.bf ); /* Eureka! Success !! At the expense of using a String-Array, ... *4* separate fread calls! We still have to resolve the value as an unsigned long, which is accomplished thru trivial pointer/casting of the contents of the unsigned char buffer space... */ } //main 6. Attempt 6,7,8,9,etc. Are just too freaky to bother posting! 7. Conclusion: Except for being "clever" at using *4* fread's or by re-arranging a unsigned char, byte-by-byte using shifts and ors, I just see NO WAY to directly read in Integer Values without being "corrupted" by Intel's expectations! what other method(s) are available to achieves this that I may of missed? ANY COMMENTS WELCOMED. Thanks.