D
D
Dutymy2021-10-26 00:33:40
C++ / C#
Dutymy, 2021-10-26 00:33:40

How to put the content of a file into a byte array at compile time?

I have an application that should show the user a long text - the text for convenient changes is stored in a file and should be inserted into an array at the compilation stage, how to do this using a macro?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rPman, 2021-10-26
@Dutymy

the file can be turned into a string using the xxd -i binary utility, and its output can be connected to include by adding something like

unsigned char data[]=
#include "xxd_result.h"
};

there are also other utilities for converting binary files to c code, Google gives out the first bin2c, plus writing such a utility yourself is a ten-minute question.
ps gcc linker can format any binary files into object files (so that they can be included during linking, and in the code access as an extern array of bytes)
https://stackoverflow.com/questions/2627004/embedd...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question