User: alciro    User
 

Programación en C++ Builder

Share |
 Arrays (matrices)
 Punteros
3. Ejemplo de clase en c++
8. Métodos de la clase AnsiString
 Proyectos en C++
 Packages, distribuir una aplicación sin instalación
 Ordenación por intercambio o burbuja
 Funciones de cadenas de caracteres string.h

5. Ficheros .INI

Descripción

5.1. Propiedades y métodos de la clase Ansistring

Count
Número de cadenas en la lista.

Introduces an abstract property to represent the number of strings in the list.

__property int Count = {read=GetCount, nodefault};

Description

Descendants of TStrings implement a Count property to indicate the number of strings in the list.

Use the Count property when iterating over all the strings in the list, or when trying to locate the position of a string relative to the last string in the list.

Names
Nombre de la variable.

Indicates the name part of strings with the form Name=Value.

__property AnsiString Names[int Index] = {read=GetName};

Description

When the list of strings for the TStrings object includes strings of the form Name=Value, read Names to access the name part of a string. Names is the name part of the string at the position indicated by Index. Index gives the position of the string, where 0 is the first string, 1 is the second string, and so on. If the string at the specified position is not of the form Name=Value, Names returns an empty string.

Strings of the form Name=Value are commonly found in .ini files. For example, here are a few strings taken from a typical .ini file:


DisplayGrid=1
SnapToGrid=1
GridSizeX=8
GridSizeY=8

The Name that identifies the string is to the left of the equal sign (=), and the current Value of the Name identifier is on the right side. There should be no spaces present before or after the equal sign.

Values
Valor de la variable.

Represents the value part of a string associated with a given Name, on strings with the form Name=Value.

__property AnsiString Values[AnsiString Name] = {read=GetValue, write=SetValue};

Description

When the list of strings for the TStrings object includes strings of the form Name=Value, use Values to get or set the value part of a string associated with a specific name part. If the list does not contain any strings of the proper Name=Value form, or if none of those strings matches the Name index, Values returns an empty string.

Note:    The Name index is case-insensitive. That is, Values is the value part for the first occurrence of Name or an equivalent string that differs only in case.

Strings of the form Name=Value are commonly found in .ini files. For example, here are a few strings taken from a typical .ini file:


DisplayGrid=1
SnapToGrid=1
GridSizeX=8
GridSizeY=8

The Name that identifies the string is to the left of the equal sign (=), and the current Value of the Name identifier is on the right side. There should be no spaces present before or after the equal sign.

IndexOfName
Índice de la variable.

Returns the position of the first string with the form Name=Value with the specified name part.

virtual int __fastcall IndexOfName(const AnsiString Name);

Description

Call IndexOfName to locate the first occurrence of a string with the form Name=Value where the name part is equal to the Name parameter or differs only in case. IndexOfName returns the 0-based index of the string. If no string in the list has the indicated name, IndexOfName returns -1.

Strings of the form Name=Value are commonly found in .ini files. For example, here are a few strings taken from a typical .ini file:


DisplayGrid=1
SnapToGrid=1
GridSizeX=8
GridSizeY=8

The Name that identifies the string is to the left of the equal sign (=), and the current Value of the Name identifier is on the right side. There should be no spaces present before or after the equal sign.

Note:    If there is more than one string with a name portion matching the Name parameter, IndexOfName returns the position of the first such string.

5.2. Ejemplo de utilización

Si disponemos una caja de texto de nombre Memo1 que contiene los datos mostrados en la siguiente tabla, podemos acceder a los valores o nombres de las variables mediante las propiedades  Names, Values.

ÍndiceContenido del Memo1
0Variable1=1
1Variable2=8
2Variable3=10

int Pos, Ind;
String Nombre, Dato;
Pos = Memo1->Lines->Count();                    //Retorna el índice máximo de las líneas (2)
Nombre = Memo1->Lines->Names[1];                //Retorna el nombre de la variable (Variable2)
Ind = Memo1->Lines->IndexOfName("Variable3");   //Retorna el índice de la variable (2)
Dato = Memo1->Lines->Values["Variable2"];       //Retorna el valor de la variable (8)
Memo1->Lines->Values["Variable2"] = 134;        //Asigna el valor 134 a la variable 2
//si no existe crea la cadena "Variable2 = 134" y la añade al final de la lista

5.2.1. Configuración de variables de entorno

Insertar un Memo oculto, (con la propiedad Visible = false)

Guardar las Variables

Memo2->Lines->Values["Top"]=Form1->Top;
Memo2->Lines->Values["Left"]=Form1->Left;
Memo2->Lines->Values["Width"]=Form1->Width;
Memo2->Lines->Values["Heigth"]=Form1->Heigth;
Memo2->Lines->Values["Color"]=Form1->Color;
Memo2->Lines->Values["DrTrabajo"]=OpenDialog1->FileName;

Para Guardar, utilizar SaveDialog1->FileName

Recuperar las Variables

OpenDialog->InitialDir=Memo2->Lines->Values["DrTrabajo"];
Form1->Top=Memo2->Lines->Values["Top"].ToInt();
Memo1->Color=Memo2->Lines->Values["Color"].ToInt()

Guardar el fichero de configuración (cuando se cierra la aplicación,

Memo2->Lines->SaveToFile("Config.cfg");

Recuperar el fichero de configuración (cuando se abre la aplicación, OnCreate o constructor)

Memo2->Lines->LoadFromFile("Config.cfg");
Loading

copyright © 2007-2024  www.alciro.org  All rights reserved.         
Share |