2

I have the following text file: g.dat

16
0.0950125098376374401853193354250
0.281603550779258913230460501460
0.458016777657227386342419442983
0.617876244402643748446671764049
0.755404408355003033895101194847
0.865631202387831743880467897713
0.944575023073232576077988415535
0.989400934991649932596154173450

0.189450610455068496285396723209
0.182603415044923588866763667977
0.169156519395002538189312079058
0.149595988816576732081501730116
0.124628971255533872052476277863
0.0951585116824927848099251053810
0.0622535239386478928628438391746
0.0271524594117540948517805723700

And the following C++ code to ready it:

ifstream In;
  In.open(("g.dat").c_str());
  In>>gaussdim;
  gt = new double[gaussdim];
  gc = new double[gaussdim];
  for(int i=0;i<gaussdim/2;i++)
  {
    In>>gt[i];
    gt[gaussdim-i-1]=-gt[i];
  }
  for(int i=0;i<gaussdim/2;i++)
  {
    In>>gc[i];
    gc[gaussdim-i-1]=gc[i];
  }
  In.close();

I would like to embed this data file into my program so that I can easily redistribute it and not be dependent on always taking care of many different files. I am targeting linux and mac so xxd would be a possibility for me to convert the data to a big char. I would need help with the next step, i.e. how to convert this char into a stream or whatever better solution there is. What do you recommend?

Edit: a lot of good answers, the chosen one has for me the advantage that the change to the code is minimal and is general enough that I can use it in other similar parts of the code where several input files with different structure are parsed.

3
  • Sorry If I couldn't got your question, but from what I understand you need this data accessible through out the life of your application, without having to read the data file.Right?In that case, what you are doing is perfectly fine. You read data one time and use it endlessly. Or you can go by the way below answer suggest. Commented May 24, 2012 at 0:31
  • Actually I want to get rid of the data file, insert it somehow as text in the code, and then change the code so that 'gc' and 'gt' finally stay with the same values as in the original code Commented May 24, 2012 at 0:41
  • But I didn't got 'then change the code', is it that your code is overwriting its own behavior that is to say it is spitting code at run time which is replacing the orignal binary.As you may have guessed, I'm puzzled at the moment by your need.Apology if you mean something simpler than this, but plz do explain.. Commented May 24, 2012 at 1:02

5 Answers 5

4

I'm guessing that you don't actually want to embed a text file in your code, so the answers by Blastfurnace and Kyle C for much more reasonable solutions to your problem.

But if you really do actually want to embed the text file in your code and then read it in as a stream, the easiest way to do that is something like this.

First, embed the text file as a string:

static const char *gdat="16\n"
"0.0950125098376374401853193354250\n"
"0.281603550779258913230460501460\n"
"0.458016777657227386342419442983\n"
"0.617876244402643748446671764049\n"
"0.755404408355003033895101194847\n"
"0.865631202387831743880467897713\n"
"0.944575023073232576077988415535\n"
"0.989400934991649932596154173450\n"
"\n"
"0.189450610455068496285396723209\n"
"0.182603415044923588866763667977\n"
"0.169156519395002538189312079058\n"
"0.149595988816576732081501730116\n"
"0.124628971255533872052476277863\n"
"0.0951585116824927848099251053810\n"
"0.0622535239386478928628438391746\n"
"0.0271524594117540948517805723700\n";

Then, instead of using ifstream("g.dat") you can use stringstream(gdat), and get a stream which is, for your purposes, effectively identical.

Sign up to request clarification or add additional context in comments.

1 Comment

If you're going to do this in multiple places, possibly with larger files, you really want to look into writing a simple code generator to do the grunt work, as described in Basile Starynkevitch's answer.
2

Put it into an array of double that you initialize. The general form is

double my_values[16] = { 1.234, 2.345, ... etc .. };

Comments

2
const double data[] = {
    0.0950125098376374401853193354250,
    0.281603550779258913230460501460 ,
    0.458016777657227386342419442983 ,
    0.617876244402643748446671764049 ,
    0.755404408355003033895101194847 ,
    0.865631202387831743880467897713 ,
    0.944575023073232576077988415535 ,
    0.989400934991649932596154173450 ,                                       
    0.189450610455068496285396723209 ,
    0.182603415044923588866763667977 ,
    0.169156519395002538189312079058 ,
    0.149595988816576732081501730116 ,
    0.124628971255533872052476277863 ,
    0.0951585116824927848099251053810,
    0.0622535239386478928628438391746,
    0.0271524594117540948517805723700
};

Comments

2

Use (or write) some program, perhaps a small awk or python or ocaml script, to transform your g.dat file into a C file.

Modify your build procedure (e.g. your Makefile) to add a dependency for that generated C file from your original g.dat file.

Compile that generated C file, and link it into your binary.

1 Comment

If I had to do something like this, and g.dat was more than a few lines long, this is exactly what I'd do. The question is which of the above answers to have your code generator output—but honestly, it doesn't really matter that much. You could generate a single big array of numbers (like Kyle C's answer) with a trivial sed one-liner, while pre-processing it into the final results (like Blastfurnace's answer) would be more like a few lines of Python, but they'd all be pretty easy.
0

This will define and initialize the gt and gc arrays similar to your current code. Note that these aren't dynamically allocated so you don't need to delete[] them when you're done.

double gt[] =
{
     0.0950125098376374401853193354250, 0.281603550779258913230460501460,
     0.458016777657227386342419442983,  0.617876244402643748446671764049,
     0.755404408355003033895101194847,  0.865631202387831743880467897713,
     0.944575023073232576077988415535,  0.989400934991649932596154173450,
    -0.989400934991649932596154173450, -0.944575023073232576077988415535,
    -0.865631202387831743880467897713, -0.755404408355003033895101194847,
    -0.617876244402643748446671764049, -0.458016777657227386342419442983,
    -0.281603550779258913230460501460, -0.0950125098376374401853193354250
};

double gc[] =
{
    0.189450610455068496285396723209,  0.182603415044923588866763667977,
    0.169156519395002538189312079058,  0.149595988816576732081501730116,
    0.124628971255533872052476277863,  0.0951585116824927848099251053810,
    0.0622535239386478928628438391746, 0.0271524594117540948517805723700,
    0.0271524594117540948517805723700, 0.0622535239386478928628438391746,
    0.0951585116824927848099251053810, 0.124628971255533872052476277863,
    0.149595988816576732081501730116,  0.169156519395002538189312079058,
    0.182603415044923588866763667977,  0.189450610455068496285396723209
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.