Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-26-2010, 10:57 PM
|
#1
|
|
Member
Registered: Jul 2010
Distribution: Lubuntu Lucid Lynx
Posts: 52
Rep:
|
Inserting text blob into a C program
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.
|
|
|
|
08-27-2010, 08:27 AM
|
#2
|
|
Senior Member
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 2,672
|
not sure why a normal file pointer declaration wouldnt work:
Code:
FILE * fstream; char temp[100];
fstream = fopen(file.txt, "r");
...
fgets(temp, 100, fstream);
would this work for you or did i misunderstand ?
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.
Last edited by schneidz; 08-27-2010 at 08:35 AM.
|
|
|
|
08-27-2010, 09:27 AM
|
#3
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
AFAIK he wants the text embedded into the executable.
|
|
|
|
08-27-2010, 01:52 PM
|
#4
|
|
Member
Registered: Jul 2010
Distribution: Lubuntu Lucid Lynx
Posts: 52
Original Poster
Rep:
|
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.
|
|
|
|
08-27-2010, 02:06 PM
|
#5
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
Last edited by MTK358; 08-27-2010 at 02:07 PM.
|
|
|
|
08-27-2010, 02:27 PM
|
#6
|
|
Member
Registered: Jul 2010
Distribution: Lubuntu Lucid Lynx
Posts: 52
Original Poster
Rep:
|
It's a command-line application (so far).
|
|
|
|
08-27-2010, 03:12 PM
|
#7
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
I wasn't saying you should use Qt (even though Qt is perfectly suited for non-GUI apps), I just made a suggestion.
|
|
|
|
08-27-2010, 06:21 PM
|
#8
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,265
|
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.
--- rod.
|
|
|
|
08-27-2010, 07:07 PM
|
#9
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
I also see nothing wrong with your Perl script solution.
|
|
|
|
08-27-2010, 07:16 PM
|
#10
|
|
Senior Member
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791
Rep:
|
Code:
#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;
}
|
|
|
0 members found this post helpful.
|
08-27-2010, 07:29 PM
|
#11
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
What's the point of that, and can't you see the OP isn't using C++?
|
|
|
|
08-28-2010, 07:59 PM
|
#12
|
|
Member
Registered: Jul 2010
Distribution: Lubuntu Lucid Lynx
Posts: 52
Original Poster
Rep:
|
Quote:
Originally Posted by Kenny_Strawn
Code:
#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.
|
|
|
|
08-28-2010, 08:05 PM
|
#13
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
I think you should just go with your Perl script.
And mark the thread as solved (in Thread Tools).
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:41 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|