求BCB操纵INI文件的函数详解,知道的兄台,先行谢过了

wangyelon 2002-08-13 05:16:31
所有看此帖的兄台(姐妹?)都是我要谢谢的!

我在写一个管理信息系统,想把界面的设置保存下来,
例如我在MainForm里放了几个各自实现一部分功能的Frame,
默认是有一个显示其他的都藏起来 ,运行时可以选需要的,
如果退出时记录所选的,下次就很直接了,ini我不熟悉.
So please!
...全文
127 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
iamleejun 2002-10-24
  • 打赏
  • 举报
回复
mark
iamleejun 2002-10-24
  • 打赏
  • 举报
回复
mark
iamleejun 2002-10-24
  • 打赏
  • 举报
回复
mark
kingcaiyao 2002-08-13
  • 打赏
  • 举报
回复
一个简单的INI文件有必要这么麻烦吗?
COKING 2002-08-13
  • 打赏
  • 举报
回复
建议你保存到xml文件里!
wangyelon 2002-08-13
  • 打赏
  • 举报
回复
上面这位大侠意思我明白,不过好向没必要这样大费周章.哈.
xiaoguoru 2002-08-13
  • 打赏
  • 举报
回复
我觉得得不一定要用TIniFile也可以操作*.ini文件,下面我给一个例子:
int count ;
count = ListView->Items->Count ;
*insertcount = count;
*totaltime = slength;

if(StrToInt(ListView->Items->Item[count-1]->SubItems->Strings[1]) >= slength)//ListView 是TListView控件.
{
int insert[2],end,begin, run = 0 ,sb = 0;
bool issame = false;

if(StrToInt(ListView->Items->Item[count-1]->SubItems->Strings[1]) >= slength) //插入文件里有的插入点大于被插入文件的长度
{

TStringList *List = new TStringList();
List->Clear();
for(int i = 0; i < count;i++)
{
insert[0] = StrToInt(ListView->Items->Item[i]->SubItems->Strings[1]);
//insert[1] = StrToInt(ListView->Items->Item[i]->SubItems->Strings[1]);
begin = StrToInt(ListView->Items->Item[i]->SubItems->Strings[2]);
end = StrToInt(ListView->Items->Item[i]->SubItems->Strings[3]);
if(insert[0] >= slength)//插入文件的插入点大于被插入文件的长度
{
for(int j = i; j < count;j++)
{
*totaltime = *totaltime + run;
List->Add(ListView->Items->Item[j]->SubItems->Strings[0]
+ ListView->Items->Item[j]->Caption);
List->Add(ListView->Items->Item[j]->SubItems->Strings[1]); //加入插入点
List->Add(ListView->Items->Item[j]->SubItems->Strings[2]);
List->Add(ListView->Items->Item[j]->SubItems->Strings[3]);
*totaltime = *totaltime + StrToInt(ListView->Items->Item[j]->SubItems->Strings[3])
- StrToInt(ListView->Items->Item[j]->SubItems->Strings[2]);
}

if(FileExists(Path+"\\InsertFilePoint.ini"))//Path是一个全局变量,它代表应用程序的路径.
{ DeleteFile(Path+"\\InsertFilePoint.ini");
List->SaveToFile(Path+"\\InsertFilePoint.ini");
}
else
List->SaveToFile(Path+"\\InsertFilePoint.ini");
break;
}
else //插入文件的插入点小于被插入文件的长度
{

List->Add(ListView->Items->Item[i]->SubItems->Strings[0]
+ ListView->Items->Item[i]->Caption);
List->Add(IntToStr(insert[0]+ run));
List->Add(IntToStr(begin));
List->Add(IntToStr(end));
if(!issame)
{
sb = insert[0];
}
run = run + end - begin;

insert[1] = StrToInt(ListView->Items->Item[i+1]->SubItems->Strings[1]);
if(insert[0] == insert[1])
{
issame = true;
}
else
{
*insertcount = *insertcount+1;
List->Add(edtSource->Text);
List->Add(IntToStr(insert[0]+run));
List->Add(IntToStr(sb));
List->Add(IntToStr(slength));
issame = false;
}

}
}
}
}
else //所有插入点均小于被插入文件的总长
{
int insert[2],end,begin, run = 0,sb = 0;
bool issame = false;
TStringList *List = new TStringList();
List->Clear();

for(int i = 0 ;i < count; i++)
{
insert[0] = StrToInt(ListView->Items->Item[i]->SubItems->Strings[1]);
begin = StrToInt(ListView->Items->Item[i]->SubItems->Strings[2]);
end = StrToInt(ListView->Items->Item[i]->SubItems->Strings[3]);
List->Add(ListView->Items->Item[i]->SubItems->Strings[0]
+ ListView->Items->Item[i]->Caption);
List->Add(IntToStr(insert[0]+ run));
List->Add(IntToStr(begin));
List->Add(IntToStr(end));
if(!issame)
{
sb = insert[0];
}
run = run + end - begin;

if(i == count-1) //最后一个
{
*insertcount = *insertcount+1;
List->Add(edtSource->Text);
List->Add(IntToStr(insert[0]+run));
List->Add(IntToStr(sb));
List->Add(IntToStr(slength));
*totaltime = *totaltime + run;
break;
}
else
{
insert[1] = StrToInt(ListView->Items->Item[i+1]->SubItems->Strings[1]);
if(insert[0] == insert[1])
{
issame = true;
}
else
{
*insertcount = *insertcount+1;
List->Add(edtSource->Text);
List->Add(IntToStr(insert[0]+run));
List->Add(IntToStr(sb));
List->Add(IntToStr(slength));
issame = false;
}
}
}

if(FileExists(Path+"\\InsertFilePoint.ini"))
{
DeleteFile(Path+"\\InsertFilePoint.ini");
List->SaveToFile(Path+"\\InsertFilePoint.ini");
}
else
{
List->SaveToFile(Path+"\\InsertFilePoint.ini");
}
}
然后在你读出来时也是一行一行读,它是零行开始的.
wangyelon 2002-08-13
  • 打赏
  • 举报
回复
帮助里例子的我看了,不过还是感谢热心人!
mygodness 2002-08-13
  • 打赏
  • 举报
回复
我的经验:
1、首先要在加入包含文件:#include <inifiles.hpp>

2、在BCB里有一个TIniFile的类,使用时可以创建这样一个类:TIniFile *ini;ini=new TIniFile(FileName);

3、在TIniFile类里,唯一的属性就是FileName,这是一个AnsiString类型的参数。

4、读INI文件时,例如读字符串可用TIniFile的以下方法:

AnsiString a;a=ini->ReadString(const AnsiString Section, const AnsiString Ident,
const AnsiString Default);Section就是分部的名称,Ident是要读的值名称,Default是读取发生错误时的默认值。

5、其它读值的方法还有:ReadBool
ReadDate
ReadDateTime
ReadFloat
ReadInteger
ReadTime
使用方法基本相同。

6、 设置值时:可用的方法有WriteString

WriteBool
WriteDate
WriteDateTime
WriteFloat
WriteInteger
WriteTime


7、以writeString为例:ini->WriteString(AnsiString Section,AnsiString Ident,AnsiString value);其中value是要写入的值。其它参数和读值时是相同的意思。

8、其它还有些比较有用的方法有ReadSection,ReadSections分别读取某部分的值名列表,和读取所有部分的值名列表。

9、使用完之后,别忘记释放资源:delete ini;
gfh21cn 2002-08-13
  • 打赏
  • 举报
回复
void __fastcall TForm1::FormCreate(TObject *Sender)

{
TIniFile *pIniFile = new TIniFile("c:\MyApp\bin\MyApp.ini");
if (pIniFile->ReadBool("AutoLoad", "FormProperties", false) == true)
{
Visible = pIniFile->ReadBool("FormOptions", "Visible", true);
Color = (Graphics::TColor)pIniFile->ReadInteger("FormOptions", "Color", clWindow);
Caption = pIniFile->ReadString("FormOptions", "Caption", "Main");
}
delete pIniFile;
}



呵呵,其实这些都是帮助里

gfh21cn 2002-08-13
  • 打赏
  • 举报
回复
#include <Inifiles.hpp>
void __fastcall TForm1::FormCreate(TObject *Sender)

{
TIniFile* pif = new TIniFile("WIN.INI");
pif->ReadSections(ListBox1->Items);
pif->ReadSection("Ports",ListBox2->Items);
pif->ReadSectionValues("Ports",ListBox3->Items);
delete pif;
}

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧