LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 08-26-2010, 10:57 PM   #1
Hidden Windshield
Member
 
Registered: Jul 2010
Distribution: Fedora
Posts: 68

Rep: Reputation: 27
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.
 
Old 08-27-2010, 08:27 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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.
 
Old 08-27-2010, 09:27 AM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
AFAIK he wants the text embedded into the executable.
 
Old 08-27-2010, 01:52 PM   #4
Hidden Windshield
Member
 
Registered: Jul 2010
Distribution: Fedora
Posts: 68

Original Poster
Rep: Reputation: 27
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.
 
Old 08-27-2010, 02:06 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Qt has a resource system.

EDIT: http://doc.qt.nokia.com/4.6/resources.html

Last edited by MTK358; 08-27-2010 at 02:07 PM.
 
Old 08-27-2010, 02:27 PM   #6
Hidden Windshield
Member
 
Registered: Jul 2010
Distribution: Fedora
Posts: 68

Original Poster
Rep: Reputation: 27
It's a command-line application (so far).
 
Old 08-27-2010, 03:12 PM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I wasn't saying you should use Qt (even though Qt is perfectly suited for non-GUI apps), I just made a suggestion.
 
Old 08-27-2010, 06:21 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 08-27-2010, 07:07 PM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I also see nothing wrong with your Perl script solution.
 
Old 08-27-2010, 07:16 PM   #10
Kenny_Strawn
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
Blog Entries: 62

Rep: Reputation: 56
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.
Old 08-27-2010, 07:29 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
What's the point of that, and can't you see the OP isn't using C++?
 
Old 08-28-2010, 07:59 PM   #12
Hidden Windshield
Member
 
Registered: Jul 2010
Distribution: Fedora
Posts: 68

Original Poster
Rep: Reputation: 27
Quote:
Originally Posted by Kenny_Strawn View Post
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.
 
Old 08-28-2010, 08:05 PM   #13
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I think you should just go with your Perl script.

And mark the thread as solved (in Thread Tools).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
inserting text using sed Atwin Programming 6 02-02-2009 11:19 PM
PHP+SQL : formatting text for presentation in a blob caged Programming 2 11-24-2004 05:48 PM
Gimp-1.3 inserting text nadine.mauch Linux - Software 0 07-26-2004 03:07 AM
Inserting Text wildcat22 Linux - General 8 04-25-2004 07:51 AM
inserting text into a file DavidPhillips Programming 5 08-15-2003 04:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration