tags:

views:

131

answers:

3

Is there anyway I can incorporate a pretty large text file (about 700KBs) into the program itself, so I don't have to ship the text files together in the application directory ? This is the first time I'm trying to do something like this, and I have no idea where to start from.

Help is greatly appreciated (:

+5  A: 

Depending on the platform that you are on, you will more than likely be able to embed the file in a resource container of some kind.

If you are programming on the Windows platform, then you might want to look into resource files. You can find a basic intro here:

http://msdn.microsoft.com/en-us/library/y3sk7e6b.aspx

With more detailed information here:

http://msdn.microsoft.com/en-us/library/zabda143.aspx

casperOne
+1 for system resource handling. On MacOS consider using the .app form for your application. On *nix systems look for a resource handler in you framework: someone else has probably done the work already...
dmckee
+1  A: 

If you can figure out how to use a resource file, that would be the preferred method.

It wouldn't be hard to turn a text file into a file that can be compiled directly by your compiler. This might only work for small files - your compiler might have a limit on the size of a single string. If so, a tiny syntax change would make it an array of smaller strings that would work just fine.

You need to convert your file by adding a line at the top, enclosing each line within quotes, putting a newline character at the end of each line, escaping any quotes or backslashes in the text, and adding a semicolon at the end. You can write a program to do this, or it can easily be done in most editors.

This is my example document:
"Four score and seven years ago,"
can be found in the file c:\quotes\GettysburgAddress.txt

Convert it to:

static const char Text[] =
"This is my example document:\n"
"\"Four score and seven years ago,\"\n"
"can be found in the file c:\\quotes\\GettysburgAddress.txt\n"
;

This produces a variable Text which contains a single string with the entire contents of your file. It works because consecutive strings with nothing but whitespace between get concatenated into a single string.

Mark Ransom
If you've never done this, do it now. Then *don't* use it in a production environment. But it is such a cheep and easy introduction to code generation that there is no excuse for not having tried it.
dmckee
I've never done this with a text file, but I've done it for moderate sized data tables.
Mark Ransom
@Mark: I assumed you'd tried it, the instruction was aimed at the OP. And BTW, I probably shouldn't have emphasized the "don't" above. Moderation in all things. Anyway, nice summary.
dmckee
This would take too much time to do since the file has about 5k lines of text, with quotes and other stuff. So I'll probably try to use resources. Thanks for help tho' (:
Ahmed
@Ahmed: *You* don't edit the file! You're a programmer, you write a tool to edit it for you. That's the whole point.
dmckee
+5  A: 

Have a look at the xxd command and its -include option. You will get a buffer and a length variable in a C formatted file.

epatel