Including binaries in Pure C

General Discussion, STOS.
User avatar
PhilC
Moderator
Moderator
Posts: 7440
Joined: 23 Mar 2018 20:22

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: 383
Joined: 08 May 2022 18:11
Location: The Midlands

Re: Including binaries in Pure C

Post by chronicthehedgehog »

PhilC wrote: 26 Jun 2025 20:53 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

Code: Select all

xxd.ttp -i infile outfile
You do not have the required permissions to view the files attached to this post.
User avatar
JezC
Posts: 2782
Joined: 28 Aug 2017 23:44

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: 7440
Joined: 23 Mar 2018 20:22

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: 2284
Joined: 03 Jun 2018 19:37

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: 820
Joined: 28 Aug 2017 23:22
Location: Kosice, Slovakia

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: 7440
Joined: 23 Mar 2018 20:22

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.

Return to “SOFTWARE”

Who is online

Users browsing this forum: ClaudeBot and 3 guests