ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm writing a C program, and using Autotools. I have a large text file that I need to include verbatim, as data for my program.
I used to have a hacked-together Perl script that would take a file like this:
Code:
A Rabbi, a Priest, and a Minister walked into a bar.
The bartender said, "What is this, a joke?"
and generate a file like this:
Code:
#define TXT_DATA \
"A Rabbi, a Priest, and a Minister walked into a bar.\n" \
"The bartender said, \"What is this, a joke?\"\n" \
""
which I would then include into every source file that needed it. Unfortunately, I managed to somehow lose my Perl script, and rather than recreate it, I wanted to learn how to do this The Right Way(tm).
I found this site that contains instructions for doing exactly what I want, but that technique requires GNU's ld, and the whole point of using Autotools in the first place is to make my project platform- and compiler-independent.
Anybody got any ideas?
Edit: I should point out that, according to the Autotools help, I can do this with a script called either "txtc.sh" or "txtc.sh.in". Unfortunately, Google can't find such a script, and it's not in any package that I can find.
Last edited by Hidden Windshield; 08-26-2010 at 11:05 PM.
edit: else you would need to emulate that in a script by cat-ing (echo \#define, your joke, then the rest of your c-file) for each file you are editing.
Yes, the text has to be embedded in the executable. Also, cat wouldn't work, since it would also need to escape quotes, add quotes to every line, add a backslash to the end of every line, etc., etc., etc.
For the moment (so the project can go ahead), I'm going to recreate my Perl script. However, I would really like to know if there's any way to do this.
Thanks!
Edit: Something like a Windows resource, for those of you familiar with Windows programming.
Last edited by Hidden Windshield; 08-27-2010 at 02:04 PM.
In my opinion, your Perl script that translates the plain text into a format suitable for inclusion as a literal string is the right way. The script shouldn't be very hard to reproduce.
#include <iostream>
using namespace std;
int main() {
cout << endl
<< 'A Rabbi, a priest and a minister walked into a bar.\n'
<< 'The bartender said "What, is this a joke?"';
return 0;
}
#include <iostream>
using namespace std;
int main() {
cout << endl
<< 'A Rabbi, a priest and a minister walked into a bar.\n'
<< 'The bartender said "What, is this a joke?"';
return 0;
}
I'm not trying to output it, it's actually data that my program needs. Also, I'm using regular C, not C++.
I've recreated my Perl script, so unless anyone has a better idea, that's what I'll use. Thanks for your help, everyone.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.