3

I have some data files which I will have to read in every simulation. I want to save this time. I can use perl to write a class out of these data files which can be compiled and used.

Is there any elegant way to do this ? - Reading data in c++ during compile time ?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 5
    You can use an `#include` directive. Alternatively you can use the platform's support for resource data in executables. – Cheers and hth. - Alf Apr 17 '16 at 09:33
  • 1
    If you have comma separated data, you could get off with a array declaration and initialize that using `#include "MyDatafile.txt"`. – πάντα ῥεῖ Apr 17 '16 at 09:34
  • 1
    http://stackoverflow.com/q/13856930/560648? – Lightness Races in Orbit Apr 17 '16 at 09:40
  • http://stackoverflow.com/questions/26161422/is-it-possible-to-read-a-file-at-compile-time – Teivaz Apr 17 '16 at 10:32
  • 1
    A nice way you can do this in C++11 is using user-defined string literals. You can often put the content of your file inside a string literal in a C++ source file, and then include this file to get the symbol. As πάντα ῥεῖ writes, if the data is already say CSV then it will be better to store it directly in an array rather than a string literal so that you don't have to parse the string yourself. – Chris Beck Apr 17 '16 at 12:30
  • Hello Chris, I liked the idea. Let me come back to u once I am done :). – Sumit Adhikari Apr 17 '16 at 16:20
  • 1
    If you go the embedded resource route, the Qt resource system provides a cross-platform framework to do so. – Nicolas Holthaus Apr 17 '16 at 17:03

0 Answers0