Reputation: 70
I've googled this for a bit but I don't seem to get anything resembling what I’m asking.
I'm creating a very simple C script which creates a bunch of template files (none of them are code or libraries or anything, they're just txt files), depending on an argument passed through the console, the way I was gonna resolve this is to just use fopen and fwrite, basically assembling the files line by line, replacing only the ones I need, but I figure there must be a way to bundle some files into the code so I can open and replace just what needs to change from case to case.
I imagine i could also create a couple of const char *file_text and that would do the trick, but I'd like to know if what I'm asking is possible should the need to use something harder to work with than text arise.
Problem is I can't seem to find how, to be clear, I want to do something like this on the console: ./Gen.sh ProjectName
And have Gen.sh be a self-contained file, with no need to keep the original templates around on the same folder.
Upvotes: 2
Views: 787
Reputation: 4750
You can use the linker to do such embedding
$ ld -r -b binary cat.png -o cat.o
The object file will have three symbols in it,
$ nm cat.o
_cat_start
_cat_end
_cat_size
To use them from C, declare some
extern
variables
extern const char cat_start;
extern const char cat_end;
extern const int cat_size;
Then you can link the generated object file ,the same can be used for text files
Upvotes: 3