INI Code BC++Builder

I am trying to get a program to read an ini my code isnt working

my ini looks like this
File.ini
1
2
3
[MyData]
Read="Hello"
Write=""


and my code is meant to read one and edit the other,
very simple interface 1 textbox and 2 buttons read and write
1
2
included this
#include "IniFiles.hpp" 

For read
1
2
3
4
5
6
7
8
9
10
11
12
13
void __fastcall TForm1::Button1Click(TObject *Sender)
{

TIniFile *inifile = NULL;
{

inifile   = new TIniFile("File.ini");
Edit1 -> Text = inifile->ReadString("MyData", "Read", "");
}
if (inifile)
delete inifile;

}

and write to add whatever is in the textbox

1
2
3
4
5
6
7
8
9
10
11
void __fastcall TForm1::Button2Click(TObject *Sender)
{
TIniFile *inifile = NULL;
{

inifile   = new TIniFile("File.ini");
inifile->WriteString("MyData", "Write", Edit1->Text);
}
if (inifile)
delete inifile;
}

i have the whole project here just in case there's something i overlooked but
ufile.io/3js5f
Used Borland C++ builder

my code isn't working but Im not getting an error message or incorrect results to go off so i cant find where things are going wrong.

it does nothing to the ini file and doesn't retrieve any data from it

What am i doing wrong?
Last edited on
Are you sure that the .ini file is in the same directory as the .exe?
Try if this example works for you: http://www.functionx.com/bcb/howto/inifile.htm
i'm certain the INI is in the same directory i tested this EXE and INI on a separate computer too

I replaced the code with the code from the example but i Cant compile it
1
2
[C++Error] Unit1.cpp(30): Cannot convert 'System::AnsiString' to 'int'.
[C++Error] Unit1.cpp(30): Type mismatch in parameter 'Value' in call to '_fastcall Inifiles::TCustomIniFile::WriteInteger(const System::AnsiString,const System::AnsiString,int)'.


added to unit1.h
1
2
AnsiString Write;
AnsiString Read;

this is the code that the example intended to prevent this, and i put it between private: and public: as shown



here is my code Unit1.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
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "IniFiles.hpp"
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TIniFile *StartUp = new TIniFile("File.ini");

Edit1 -> Text = StartUp->ReadInteger("MyData", "Read", "");

    delete StartUp;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    TIniFile *StartUp = new TIniFile("File.ini");

  StartUp->WriteInteger("MyData", "Write", Edit1->Text);

    delete StartUp;
}
//---------------------------------------------------------------------------


tried reverting to ReadString from ReadInteger but it didn't have any affect what am i missing?
Last edited on
I tried playing around with things in different versions of C++Builder
To be sure it was reading the right file I gave mine a different name, "My_test.ini".

To make testing easier, I added a ListBox to the form.
Then in the Form loading, added code to read either the sections, or the list of items in that section. Then I discovered I had to change the filename. I put a "./" in front of the name, to tell it to start from the current directory.

I also added code for each button separately. Then it seemed to work.

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
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
	TIniFile *ini = new TIniFile( "./My_test.ini" );
 //	ini->ReadSections(ListBox1->Items);
	ini->ReadSection("MyData", ListBox1->Items);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{

	TIniFile *inifile = new TIniFile("./My_test.ini");

	Edit1->Text = inifile->ReadString("MyData", "Read", "");

	if (inifile)
		delete inifile;

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
	TIniFile *inifile = new TIniFile("./My_test.ini");

	inifile->WriteString("MyData", "Write", Edit1->Text);

	if (inifile)
		delete inifile;
}
//--------------------------------------------------------------------------- 


By the way, did you know you can get an up-to-date Starter edition of C++Builder free of charge?
https://www.embarcadero.com/free-tools

Also a stand-alone C++11 compliant compiler too.
Last edited on
You've saved me!,
this works perfectly


i will be putting the source code up when ive done my program so i will be sharing that link.

i am very grateful
Edit1 -> Text = StartUp->ReadInteger("MyData", "Read", "");
If you want to read an integer you can't supply a string as default value.

StartUp->WriteInteger("MyData", "Write", Edit1->Text);
If you want to write an integer you can't use a string. C++Builder is not so smart to convert a string to an int.
Topic archived. No new replies allowed.