Read variable length binary files into memory???

Posted on 2003-02-28
Medium Priority
Last Modified: 2013-11-15
I'm teaching myself C, for Cocoa later on, so _please_ forgive my C newbie factor of 10. :)

I need to read variable lengths of the rest of a binary files (typically 80k-150k), from a variable offset in the file, and can't figure out how to do this.

In my admittedly lacking mind, here's how it should work:

i = fread(&fnsz, 4, 1, fp);
fnsz = NSSwapInt(fnsz);

const void *act_file;

act_file = malloc(fnsz);

i = fread(&act_file, fnsz, 1, fp);

Thanks in advance, for any help, more C tutorials, directions, etc.
Question by:CocoaNewbOnOSX
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 46

Expert Comment

by:Kent Olsen
ID: 8041387

Close.  Just a couple of minor things to keep you moving.

'act_file' is not a 'const'.  It's a pointer that you're going to change by placing in it the address returned by malloc().  The buffer that it will point to will also be changed when you do the read ().

The address passed to fread() is 'act_file', not '*act_file'.  'act_file' contains the address of the buffer and that's what you need to pass to fread ();

Good Luck


Accepted Solution

Kocil earned 150 total points
ID: 8041421
Yes, you have an excelent mind.
Only a little bit correction

i = fread(&fnsz, 4, 1, fp);
fnsz = NSSwapInt(fnsz); // I assume this return the length of the rest

const char *act_file;  // use char*, not void*
act_file = malloc(fnsz);

i = fread(act_file, fnsz, 1, fp); // don't use & on act_file
// it is already a pointer

// process the act_file, then dont forget to free it


This code will work on Windows or Linux, but not on DOS because DOS can malloc no more than 64K memory.



Author Comment

ID: 8069261
Thank you both for your help, you people rock.

Kocil: NSSwapInt is an Apple macro that converts from little endian to big endian.

Here's the code at this moment. After reading it, I coerce it into an NSData object for the rest of my needs. Thanks you both, again.

... //parse to relevant int

i = fread(&fnsz, 4, 1, fp); //then read it
fnsz = NSSwapInt(fnsz); //swap vyte order (comes from PC sys)

char *act_file; //NOT a constant!!! ;-)

act_file = malloc(fnsz); //alloc w/ NO pointers, thanks for tip!

i = fread(act_file, fnsz, 1, fp); //read and create data object from it...

NSData *tempData = [NSData dataWithBytes:act_file length:fnsz];


Author Comment

ID: 8078798
Kocil: I must admit, I didn't understand how this site worked. So, you should have definetly gotten more than 5 points and also an A!! Sorry about that. :-)

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Curious about the latest ransomware attack? Check out our timeline of events surrounding the spread of this new virus along with tips on how to mitigate the damage.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question