Can all registered uses please login, even just for a few minutes..
It helps build a picture where our "good traffic" is coming from..
Thanks :)

Including binaries in Pure C

General Discussion, STOS.
Post Reply
User avatar
PhilC
Moderator
Moderator
Posts: 7534
Joined: Fri Mar 23, 2018 8:22 pm

Including binaries in Pure C

Post by PhilC »

Hi All,

Struggling a bit with something simple in Pure C of all things.

I need to import a raw image file into either the C or ASM. Now I'm used to using something like:

Code: Select all

 
 Filelocation:
 incbin "name of file.raw"
 
but in PureC asm that doesn't exist.I've tried using .incbin in the C section but all the examples I've found don't seem to work.

Could someone give me some examples on how to do this using Pure C. Preferably in ASM but C will also do as long as its globally available.
If it ain't broke, test it to Destruction.
User avatar
chronicthehedgehog
Site sponsor
Site sponsor
Posts: 384
Joined: Sun May 08, 2022 6:11 pm
Location: The Midlands
Contact:

Re: Including binaries in Pure C

Post by chronicthehedgehog »

PhilC wrote: Thu Jun 26, 2025 8:53 pm Hi All,

Struggling a bit with something simple in Pure C of all things.

I need to import a raw image file into either the C or ASM. Now I'm used to using something like:

Code: Select all

 
 Filelocation:
 incbin "name of file.raw"
 
but in PureC asm that doesn't exist.I've tried using .incbin in the C section but all the examples I've found don't seem to work.

Could someone give me some examples on how to do this using Pure C. Preferably in ASM but C will also do as long as its globally available.
There's no mention of incbin in the PureC doc I have. Does an INCLUDE within a DATA section work?
In C, I'd use a tool like xxd to convert it into C array format. Here's a TOS version that might work (not tested it tbh) if you don't want to use it on linux :)
XXD.zip
(12.13 KiB) Downloaded 20 times

Code: Select all

xxd.ttp -i infile outfile
User avatar
JezC
Posts: 2820
Joined: Mon Aug 28, 2017 11:44 pm

Re: Including binaries in Pure C

Post by JezC »

Not related to the Atari ecosystem in any way, but for our ARM-based products at work, we often use a utility from Segger (www.segger.com) that converts a binary file into a C array (source & header file) - so maybe that is an option?

It's bin2c.exe - and it's listed as a free utility on their website.
User avatar
PhilC
Moderator
Moderator
Posts: 7534
Joined: Fri Mar 23, 2018 8:22 pm

Re: Including binaries in Pure C

Post by PhilC »

Thanks @chronicthehedgehog & @JezC . I'll give the bin 2c a go later on.
If it ain't broke, test it to Destruction.
User avatar
mrbombermillzy
Moderator
Moderator
Posts: 2365
Joined: Sun Jun 03, 2018 7:37 pm

Re: Including binaries in Pure C

Post by mrbombermillzy »

If your file was e.g. a bitmap image, I think you can use:

FILE* name = fopen("path/imagename.bmp", "rb");

and then reference 'name' where you need to.

So you could e.g. read the image header with:

fread(imageHeader, sizeof(unsigned char), 54, name);

(you would also have to define 'imageHeader' in the above example)

For reference, I found more info:

Code: Select all

[ fopen ]

#include <stdio.h>

FILE* fopen( const char *filename, const char *mode);

  <filename> is a string containing a filename (see Pathnames).
    If no file by that name exists, a new one is created. 
  <mode> is a string containing the read/write options.


Function opens a file and returns a FILE pointer to be used by other
I/O functions.  It returns NULL if unsuccesful.

Note: Streams stdin, stdout, stderr, stdaux and stdprn are opened
      automatically.

See also Standard I/O, freopen() and tmpfile.
from Thorsten Ottos site here: https://tho-otto.de/hypview/hypview.cgi ... 8&index=37
mikro
Posts: 826
Joined: Mon Aug 28, 2017 11:22 pm
Location: Kosice, Slovakia
Contact:

Re: Including binaries in Pure C

Post by mikro »

xxd is the way to go (there were also Atari tools for that task available, if you insist on real hardware usage).

Good practice (when cross compiling) is to make 'xxd' as part of the 'make' process -- so when you update your binary, it will get automatically converted and included.
User avatar
PhilC
Moderator
Moderator
Posts: 7534
Joined: Fri Mar 23, 2018 8:22 pm

Re: Including binaries in Pure C

Post by PhilC »

Thanks for the suggestion @mikro, I'll likely use that method when I've got a proper project to share
If it ain't broke, test it to Destruction.
Post Reply

Return to “SOFTWARE”