Hi, In C++ how do I get information from a configuration file and put it in variables.
A file like this one:

#
#	The number of inputs for each epoch
#	Default value if omitted: 60
#
InputNumber	60


#
#	The maximum epochs allowed in the network to converge
#	Default value if omitted: 300
#
MaxEpochs	300


#
#	The number of neurons in the input layer
#	Default value if omitted: 19
#
InputNeurons	19


#
#	The number of neurons in the hidden layer
#	Default value if omitted: 22
#
HiddenNeurons	22


#
#	The number of neurons in the output layer
#	Default value if omitted: 1
#
OutputNeurons	1


#
#	Default value if omitted: 0.01
#
LearningRate	0.01


#
#	The dimension of each input
#	Default value if omitted: 48
#
InputDim	48

Inside the # are only comments

Read the file line by line. Ignore empty lines and those beginning with #.
Attempt to parse other lines into [string][whitespace][value].
Attempt to match a particular string to associate with a variable.
Assign the value to the variable if the string matches.

Read the file line by line. Ignore empty lines and those beginning with #.
Attempt to parse other lines into [string][whitespace][value].
Attempt to match a particular string to associate with a variable.
Assign the value to the variable if the string matches.

How to ignore the empty lines and those beginning with #?

If the first character is #, don't continue on to parsing.
If the string is equal to "", don't continue on to parsing.

Like this, the comments I don't know how to tranlate to code :sad:

// open the file
	stream = fopen( "file.conf", "r" );
	if( stream == NULL )
	{
		printf( "The file 'file.conf' was not opened\n" );
		exit(1);
	}

	// Read the file line by line. Ignore empty lines and those beginning with #.
	while (fscanf( stream, "%d") != EOF)
	{
		if ((getc == "#") || (getc == "")
			// goto next line
		else
		{
			// Attempt to parse other lines into [string][whitespace][value].
			
			// Attempt to match a particular string to associate with a variable.

			// Assign the value to the variable if the string matches.	
		}		
	}

If you're using C, use fgets to read a line.

#include <stdio.h>

int main(void)
{
   static const char filename[] = "file.txt";
   FILE *file = fopen(filename, "r");
   if ( file != NULL )
   {
      char line[100], name[50];
      double value;
      while ( fgets(line, sizeof line, file) != NULL )
      {
         if ( line[0] == '#' || line[0] == '\n' )
         {
            continue;
         }
         if ( sscanf(line, "%49s %lf", name, &value) == 2 )
         {
            printf("name = \"%s\", value = %g\n", name, value);
         }
      }
      fclose(file);
   }
   return 0;
}

/* my output
name = "InputNumber", value = 60
name = "MaxEpochs", value = 300
name = "InputNeurons", value = 19
name = "HiddenNeurons", value = 22
name = "OutputNeurons", value = 1
name = "LearningRate", value = 0.01
name = "InputDim", value = 48
*/
This article has been dead for over six months. Start a new discussion instead.