Homework Assistance: Working with pointers to read/edit/write an .ini file

Below are the files that I have. Most of everything below ConfigINI.cpp was given and have minimal edits by me (ConfigINI.cpp, configINI.h and ConfigStuff.cpp have the edits, everything else is as it was given to me).

The program should run (no arguments), read in an INI file [simulator.ini -- not included], display two values from the ini file, make two changes, and then 'edit' or write out the ini file. This is to be done with pointers and arrays.

I'm not exactly sure what to do, nor what the pertinent information is when I search google. I have quite a bit figured out, but my issue holding me up right now is assigning the value of the parameter to the pointer in ConfigINI::Set() so that main can print the value out and make the change.

ConfigINI.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "ConfigINI.h"
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

ConfigINI::ConfigINI(): numberOfComments(0), numberOfParameters(0) {}

ConfigINI::ConfigINI(const char* filename) 
: numberOfComments  (0),
  numberOfParameters(0)
{
	Load(filename);
}

ConfigINI::~ConfigINI() {}

void ConfigINI::Load(const char* name)
{
   
  // Store filename for later Save()
  filename = name;
  
  // Read each line of text in the ini file
  // and store the Parameter/Value pairs in the arrays
  // If you find a comment, store it separately
	
  /* Variables */
  string readLine;
  string paramName, paramValue, comment;
  size_t found_eq, found_lb;
	
  /* Code */
  // set file for reading
  std::ifstream inFile;
    
  // open file, use parameters 
  inFile.open (filename.c_str(), ifstream::in);
    
  // set counters to zero
  numberOfComments = 0;
  numberOfParameters = 0;
    
  // Loop through file, read into memory
  while (inFile)
  {
    getline(inFile, readLine);
    
    // Check to see if line is a comment
    if ( readLine[0] == '#' )
    {
      comment = readLine;
//      std::cout << "This is a comment --> " << comment << std::endl;
      comments[numberOfComments]=comment;
      numberOfComments++;
    }

        
    // Check to see if line is a parameter
    found_eq = readLine.find('=');
    found_lb = readLine.find('#');
      
    if ( found_eq != string::npos )
    {
      // Get first part and second part based on the equal sign
      paramName = readLine.substr( 0,readLine.find("=") );
      paramValue = readLine.substr( readLine.find("=")+1 );
           
//      std::cout << "Parameter: " << paramName << std::endl;
//      std::cout << "Value:     " << paramValue << std::endl;
            
      iniParameter[numberOfParameters] = paramName;
      iniValue[numberOfParameters] = paramValue;
            
      numberOfParameters++;         
    }

    // print out results -- for testing purposes
    // std::cout << readLine << std::endl;
  }
    
  // close file from reading
  inFile.close();
}

void ConfigINI::Save()
{
  /* Variables */
  int i;
    
  /* Code */
  // set file for reading
  std::ofstream outFile;
    
  // open file, use parameters 
  outFile.open (filename.c_str(), ofstream::out);
    
  // write to file
  for (i = 0; i < numberOfComments; i++)
  {
    outFile << comments[i] << std::endl;
  }

  for (i = 0; i < numberOfParameters; i++)
  {
    outFile << iniParameter[i] 
            << "=" << iniValue[i] << std::endl;
  }
    
  // close file for writing
  outFile.close();
}

void ConfigINI::Set(const char* inistring,int* value)
{
  /* variables */
  int i;
  int pValue;
    
  // loop through to find inistring in iniParameter
  for (i=0; i < numberOfParameters; i++)
  {
    if (inistring == iniParameter[i])
    {
      stringstream convert(iniValue[i]);
      convert >> pValue
      *value = &pValue;
      break;
    }
  }
    
  std::cout << arrParams[i] << " " << inistring << std::endl;
  std::cout << value << " " << arrValues[i] << " " << &iniValue[i] << std::endl;
}

-----------------------------------------
ConfigINI.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#ifndef _CONFIGINI__H_
#define _CONFIGINI__H_

#include <string>

#define MAX_INI_PARAMETERS 100
#define MAX_COMMENTS        20

using std::string;
using std::ifstream;
using std::ofstream;

class ConfigINI
{
public:

  ConfigINI();
  ConfigINI(const char* filename);
  ~ConfigINI();

  void Load(const char* filename);
  void Save();

  void Set(const char* inistring,string* value);
  void Set(const char* inistring,int*    value);
  void Set(const char* inistring,float*  value);

private:

  string filename; // store the file name for the Save()

  // We have a requirement to preserve comments
  string comments[MAX_COMMENTS];
  int numberOfComments; // don't run past the end of the array!!!

  // student must create storage for all data here
  string* arrValues[MAX_INI_PARAMETERS];
  string* arrParams[MAX_INI_PARAMETERS];
    
  // Note: these 2 arrays are the ini file
  // broken out into their component parts
  string iniParameter[MAX_INI_PARAMETERS];
  string iniValue    [MAX_INI_PARAMETERS];
  int numberOfParameters;

};

#endif 

-----------------------------------------
ConfigStuff.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "ConfigStuff.h"

ConfigStuff::ConfigStuff() {}

ConfigStuff::ConfigStuff(const char* filename)
{
  // This one will call "ConfigStuff::Load()"
  Load(filename);
}

ConfigStuff::~ConfigStuff()
{
}

void ConfigStuff::Load(const char* filename)
{
  // Execute this to invoke the Base Class
  // "Load()" function to get the config file
  // loaded into the Parameter/Value pairs
  ConfigINI::Load(filename);

  // Put a series of "Set(const char* inistring,...)"
  // calls here to load each of the configuration values
  // and the pointers to those values
  Set("srcPort", &srcPort);
  Set("delayTime", &delayTime);
  Set("defaultPortSpeed", &defaultPortSpeed);
}

-----------------------------------------
ConfigStuff.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef _CONFIGSTUFF__H_
#define _CONFIGSTUFF__H_

#include "ConfigINI.h"

class ConfigStuff : public ConfigINI
{
public:

  ConfigStuff ();
  ConfigStuff (const char* filename);
  ~ConfigStuff ();
  
  void Load(const char* filename);

  int    srcPort;
  int    delayTime;
  int    defaultPortSpeed;

private:
    
};

-----------------------------------------
TestConfig.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

#include "ConfigStuff.h"

using namespace std;

int main()
{

	ConfigStuff config;

	try
	{
		config.Load("simulator.ini");

		// When you run this and the
		// srcPort and delayTime are
		// not initialized they will
		// display undefined values
		cout << config.srcPort << endl;
		cout << config.delayTime << endl;

		config.delayTime = 10;
		config.defaultPortSpeed = 2400;

		config.Save();
	}
	catch(const char* s)
	{
		cerr << s << endl;
	}

	return 0;
}
Last edited on
Bump
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void ConfigINI::Set(const char* inistring,int* value)  //value is a pointer, why not call it pValue
{
 
  int i;
  int pValue; //This is not a pointer, this name is misleading
    
 //...
  {
    if (inistring == iniParameter[i]) //This is doing a character comparison (if what you are comparing is a string use strcmp(inistring, iniParameter[i]) == 0
    {
      stringstream convert(iniValue[i]);
      convert >> pValue
      *value = pValue; //See my changes here... removed &
      break;
    }
  }
    
  //...
}
Last edited on
Thank you, I'll try that out ...

The function ConfigINI::Set( ... ) itself was what I was given. I had no hand in writing that.

At the time I posted, I was frustrated and just trying anything at that point, thus the variable name pValue -- which in my mind was Parameter Value, just something that I could convert from a string to an integer so that I could assign back to 'int* value'.

So - it seems to me I was on a basic correct track, just a little off on execution? Or am I still totally f'ed on this.

However thank you for the hint about strcmp -- I'll look that up and make sure I understand what it is doing.

I'll work on this some more and post more results as I get stuck, but I appreciate your taking the time to look at it.

Thank you again,
AT
Last edited on
A SEMICOLON -- A FRIGGIN' SEMICOLON.

I had been missing that - I was sure I had tried the cianmjc's solution, but tried it again -- and then I caught it. Grrrrr -- that's what I get for being tired and trying to code.

Okay -- I'm fresh, continuing on.

Thank you again, cianmjc.
Topic archived. No new replies allowed.