LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Inserting text blob into a C program (http://www.linuxquestions.org/questions/programming-9/inserting-text-blob-into-a-c-program-828728/)

Hidden Windshield 08-26-2010 11:57 PM

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.

schneidz 08-27-2010 09:27 AM

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.

MTK358 08-27-2010 10:27 AM

AFAIK he wants the text embedded into the executable.

Hidden Windshield 08-27-2010 02:52 PM

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.

MTK358 08-27-2010 03:06 PM

Qt has a resource system.

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

Hidden Windshield 08-27-2010 03:27 PM

It's a command-line application (so far).

MTK358 08-27-2010 04:12 PM

I wasn't saying you should use Qt (even though Qt is perfectly suited for non-GUI apps), I just made a suggestion.

theNbomr 08-27-2010 07:21 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.

--- rod.

MTK358 08-27-2010 08:07 PM

I also see nothing wrong with your Perl script solution.

Kenny_Strawn 08-27-2010 08:16 PM

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;
}


MTK358 08-27-2010 08:29 PM

What's the point of that, and can't you see the OP isn't using C++?

Hidden Windshield 08-28-2010 08:59 PM

Quote:

Originally Posted by Kenny_Strawn (Post 4080170)
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.

MTK358 08-28-2010 09:05 PM

I think you should just go with your Perl script.

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


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