0

I want to ask if the following idea is possible:

I would like to use the variables names and their values stored in the file as follows:

tcmb = 2.73 

outmap = output/all_skies_map.fits 

I would like to read the file and store those values (i.e. tcmb = 2.73 store the value 2.73 in a variable called tcmb or for instance: outmap = output/time_ordering_information.fits, i want to save the last part: time_ordering_information.fits in an string variable called outmap )

Is that possible?, Ok what i have done so far is:

ifstream input(inifile.c_str());
  char line[4048];

  char *var = NULL;
  char *value = NULL;

  int cnt = 0;
  while (!input.eof()){
  input.getline(line, 4048);
  if (input.eof())
  break;

  sscanf(line,"%s %*c %s \n", var, value);

  if(strstr(var, "clsf"      ) == var){clsfile   = value; ascii=false;}
  if(strstr(var, "map"       ) == var){fname0    = value; ascii=false;}
  if(strstr(var, "poin"      ) == var){pointing  = value; ascii=true;}
  if(strstr(var, "angle"     ) == var){angle_bol = value; ascii=false;}
  if(strstr(var, "outcrossc" ) == var){fname1    = value;}
  if(strstr(var, "outstokes" ) == var){fname2    = value;}
  if(strstr(var, "outmap"    ) == var){fname3    = value; ascii=true;}
  if(strstr(var, "outoi"     ) == var){fname4    = value;}
  if(strstr(var, "velo"      ) == var){velo      = atof(value);}
  if(strstr(var, "bgal"      ) == var){bgal      = atof(value);}
  if(strstr(var, "lgal"      ) == var){lgal      = atof(value);}
  if(strstr(var, "tcmb"      ) == var){tcmb      = atof(value);}
  if(strstr(var, "freq"      ) == var){freq      = atof(value);}
  if(strstr(var, "poin_m"    ) == var){poin_m    = atoi(value);}
  if(strstr(var, "max_l"     ) == var){max_l     = atoi(value);}
  if(strstr(var, "nskies"    ) == var){nskies    = atoi(value);}


  cerr << var << endl;



  cnt++;

  }

  input.close();

But i get Segmentation fault (core dumped) error when i try to run the entire code, i know that is crashing at this point because i put outputs after and before this piece of code.

  • It's not possible to make variables like this. All variables must be defined in your source code before the program is compiled. You can use a [map](https://en.cppreference.com/w/cpp/container/map) or [unordered_map](https://en.cppreference.com/w/cpp/container/unordered_map) to do something similar if you need to obtain the names at runtime. – Chris Rollins Jul 12 '19 at 17:46
  • yes i mean i already have the variable tcmb for instance, but i need to assign the value that is stored inside this file into this variable. and also the same for the string one – Esteban Chalbaud Jul 12 '19 at 17:50
  • basically what i am trying to figure out is a way to fill variables inside a program with values inside a file, like many .ini files do, but i do not how to do that – Esteban Chalbaud Jul 12 '19 at 18:02
  • You might have an easier time keeping the order of your variables the same and reading them in one at a time to variables in your code in the same order. This might help: https://stackoverflow.com/questions/7868936/read-file-line-by-line-using-ifstream-in-c – Hoog Jul 12 '19 at 18:05
  • You might also want to try this, if you don't mind using Boost: https://www.boost.org/doc/libs/1_65_1/doc/html/property_tree/parsers.html#property_tree.parsers.ini_parser – jjramsey Jul 12 '19 at 18:06
  • You might be looking for std::map. – n. m. could be an AI Jul 12 '19 at 20:36

1 Answers1

0

You can read in a file, and then grab values in that file and assign them to pre-exisiting variables in the code. You can use something like getline. If you don't know the order of your variables but they have names in the outside file. Maybe an array would be helpful? you can create a struct with name and value for each spot. Read them in and then assign those based on the name to the pre-existing variables you created. Another option is to search the file for specific words and then assign them. It depends on what route you want to take.

Peter
  • 1
  • 1
  • Usually that is what the array **argv in the main function does right? But i don't understand why the last piece of code with the sscanf and the loops does not work and i get a segmentation fault. – Esteban Chalbaud Jul 12 '19 at 22:01
  • do you use the **argv in main to give a file name? or to run the whole function? – Peter Jul 12 '19 at 22:18
  • no i mean i can evaluate the variables in this way ./program -tmcb 2.73 -point lib/directory I am trying not to use this approach because apart from these two parameters i have a bunch more, and is better to put all of them inside a file and load the file afterwards – Esteban Chalbaud Jul 12 '19 at 22:31
  • if you have more variables that need to be read and assigned values from a file specified in the command line, the above is the easiest way of doing it or a map like the person before suggested. Good luck! – Peter Jul 13 '19 at 11:36