1

I have a text file (>50k lines) of ascii numbers, with string identifiers, that can be thought of as a collection of data vectors. Based on user input, the application only needs one of these data vectors at runtime.

As far as I can see, I have 3 options for getting the information from this text file:

  1. Keep it as a text file, extract the required vector at run-time. I believe the downside is that you can't have a relative path in the code, so the user would have to point to the file's correct location (?). Or alternatively, get the configure script to inject the absolute path as a macro.
  2. Convert it to a static unsigned char using xxd (as explained here) and then include the resulting file. Downside is that a 5MB file turns into a 25MB include file. Am I correct in thinking that this 25MB is loaded into memory for the duration of the runtime?
  3. Convert it to an object and link using objcopy as explained here. This seems to keep the file size about the same -- are there other trade-offs?

Is there a standard/recommended method for doing this? I can use C or C++ if that makes a difference.

Thanks.

(Running on linux with gcc)

Community
  • 1
  • 1
BrT
  • 619
  • 1
  • 5
  • 15
  • Um why can't you have a relative path in the code? Filenames such as "../data/datafile.dat" are perfectly valid (assuming unix, other operating systems are similar). – jcoder Aug 10 '12 at 11:42
  • Yup, Windows supports relative paths as well. (Finally something those Unix people can't feel smugly superior about!) – Cody Gray - on strike Aug 10 '12 at 11:43
  • As far as I am aware, relative paths are relative to the directory where the program is executed from -- thus, if you change the directory from which you run, the relative path is no longer accurate. – BrT Aug 10 '12 at 11:45
  • Simpler solution, have an environment variable storing the file name or pass it as a argument to the binary (or both). – Ed Heal Aug 10 '12 at 11:47

3 Answers3

3

I would go with number 1 and pass the filepath into the program as an argument. There's nothing wrong with doing that and it is simple and straight-forward.

  • Used the make file to get the compilation directory, and then a relative file path in the code. Thanks. – BrT Aug 10 '12 at 12:52
0

You should have a look at the answers here:

Directory of running program

The top voted answer gives you a glue how to handle your data file. But instead of the home folder I would suggest to save it under /usr/share as explained in the link.

Community
  • 1
  • 1
AngelM1981
  • 141
  • 5
0

I'd preffer to use zlib (and both ways are possible:side file or include with compressed data).