| | |
Please help on how to read a file
![]() |
•
•
Join Date: Apr 2009
Posts: 2
Reputation:
Solved Threads: 0
Please can someone help me out on an issue that I ma having with delphi.
Aim is to read an INI file then take certain values only and display them under listbox or memo.
Lets say for an example my ini files looks like this
[setup]
Device =
Sector=
[set]
Anyname=
Only values I want to read is device, sector and anyname without the ‘=’
I tried with TIniFile and text, what I am getting stuck is how to identify the [setup], [set] and the ‘=’ to ignore and take the next value.
When the files is read it is at the EOF, am I correct!! If this is the case I need to decrement it to the start of the page, then create a condition to compare [setup] , [set] and the =. Then with the condition true, I need to increment by 1 (or delete the char or the string).
My question to you is, how do I decrement and inc, as I get an error with int and string.
Is this the correct approach, or do you recommend a different approach (maybe read the file as a string) as they increment compare the char then remove
Please advice
Thx in adv
Aim is to read an INI file then take certain values only and display them under listbox or memo.
Lets say for an example my ini files looks like this
[setup]
Device =
Sector=
[set]
Anyname=
Only values I want to read is device, sector and anyname without the ‘=’
I tried with TIniFile and text, what I am getting stuck is how to identify the [setup], [set] and the ‘=’ to ignore and take the next value.
When the files is read it is at the EOF, am I correct!! If this is the case I need to decrement it to the start of the page, then create a condition to compare [setup] , [set] and the =. Then with the condition true, I need to increment by 1 (or delete the char or the string).
My question to you is, how do I decrement and inc, as I get an error with int and string.
Is this the correct approach, or do you recommend a different approach (maybe read the file as a string) as they increment compare the char then remove
Please advice
Thx in adv
(Assuming the order of the elements will never be changed)
> You could read the file line by line ...
(and every time you put the line into a string)
> Then you only have to scan the string for the character '=' and read out the value after it ...
Hope this helps !
> You could read the file line by line ...
(and every time you put the line into a string)
> Then you only have to scan the string for the character '=' and read out the value after it ...
Hope this helps !
"You can't build a reputation on what you are going to do."
Hi "1shadow1" 
What about the delphi help?If you search you find.
Use the TIniFile keyword
Unit
IniFiles
Description
TIniFile enables handling the storage and retrieval of application-specific information and settings in a standard INI file. The INI file text format is a standard introduced in Windows 3.x for storing and retrieving application settings from session to session. An INI file stores information in logical groupings, called “sections.” For example, the WIN.INI file contains a section called “[Desktop]”. Within each section, actual data values are stored in named keys. Keys take the form:
<keyname>=<value>
A FileName is passed to the TIniFile constructor and identifies the INI file that the object accesses.
A related object, TMemIniFile, works the same way as TIniFile, but buffers writes in memory to minimize disk access.
Tip: You may choose to store information using the system registry instead of INI files. In this case, you can use TRegistryIniFile (which shares a common ancestor with TIniFile and so can be used in common code) or TRegistry.

What about the delphi help?If you search you find.
Use the TIniFile keyword
Unit
IniFiles
Description
TIniFile enables handling the storage and retrieval of application-specific information and settings in a standard INI file. The INI file text format is a standard introduced in Windows 3.x for storing and retrieving application settings from session to session. An INI file stores information in logical groupings, called “sections.” For example, the WIN.INI file contains a section called “[Desktop]”. Within each section, actual data values are stored in named keys. Keys take the form:
<keyname>=<value>
A FileName is passed to the TIniFile constructor and identifies the INI file that the object accesses.
A related object, TMemIniFile, works the same way as TIniFile, but buffers writes in memory to minimize disk access.
Tip: You may choose to store information using the system registry instead of INI files. In this case, you can use TRegistryIniFile (which shares a common ancestor with TIniFile and so can be used in common code) or TRegistry.
Don't be ungrateful.
When you ask something,and got the right answer,then at the bottom of the page of your question ,there is a writting : mark as solved ,so please, press that string to confirm as solved.Thank you ,in the name of the community...
Farewell...
When you ask something,and got the right answer,then at the bottom of the page of your question ,there is a writting : mark as solved ,so please, press that string to confirm as solved.Thank you ,in the name of the community...
Farewell...
I see,that you want to more....
The ini file format is still popular, many configuration files (such as the DSK Desktop settings file) are in this format. This format is especially useful in cross-platform applications, where you can't always count on a system Registry for storing configuration information. BaseCLX provides two classes, TIniFile and TMemIniFile, to make reading and writing ini files very easy.
TIniFile works directly with the ini file on disk while TMemIniFile buffers all changes in memory and does not write them to disk until you call the UpdateFile method.
When you instantiate the TIniFile or TMemIniFile object, you pass the name of the ini file as a parameter to the constructor. If the file does not exist, it is automatically created. You are then free to read values using the various read methods, such as ReadString, ReadDate, ReadInteger, or ReadBool. Alternatively, if you want to read an entire section of the ini file, you can use the ReadSection method. Similarly, you can write values using methods such as WriteBool, WriteInteger, WriteDate, or WriteString.
Following is an example of reading configuration information from an ini file in a form's OnCreate event handler and writing values in the OnClose event handler.
Each of the Read routines takes three parameters. The first parameter identifies the section of the ini file. The second parameter identifies the value you want to read, and the third is a default value in case the section or value doesn't exist in the ini file. Just as the Read methods gracefully handle the case when a section or value does not exist, the Write routines create the section and/or value if they do not exist. The example code creates an ini file the first time it is run that looks like this:
[Form]
Top=100
Left=100
Caption=Default Caption
InitMax=0
On subsequent execution of this application, the ini values are read in when the form is created and written back out in the OnClose event.
The ini file format is still popular, many configuration files (such as the DSK Desktop settings file) are in this format. This format is especially useful in cross-platform applications, where you can't always count on a system Registry for storing configuration information. BaseCLX provides two classes, TIniFile and TMemIniFile, to make reading and writing ini files very easy.
TIniFile works directly with the ini file on disk while TMemIniFile buffers all changes in memory and does not write them to disk until you call the UpdateFile method.
When you instantiate the TIniFile or TMemIniFile object, you pass the name of the ini file as a parameter to the constructor. If the file does not exist, it is automatically created. You are then free to read values using the various read methods, such as ReadString, ReadDate, ReadInteger, or ReadBool. Alternatively, if you want to read an entire section of the ini file, you can use the ReadSection method. Similarly, you can write values using methods such as WriteBool, WriteInteger, WriteDate, or WriteString.
Following is an example of reading configuration information from an ini file in a form's OnCreate event handler and writing values in the OnClose event handler.
delphi Syntax (Toggle Plain Text)
procedure TForm1.FormCreate(Sender: TObject); var Ini: TIniFile; begin Ini := TIniFile.Create( ChangeFileExt( Application.ExeName, '.INI' ) ); try Top := Ini.ReadInteger( 'Form', 'Top', 100 ); Left := Ini.ReadInteger( 'Form', 'Left', 100 ); Caption := Ini.ReadString( 'Form', 'Caption', 'New Form' ); if Ini.ReadBool( 'Form', 'InitMax', false ) then WindowState = wsMaximized else WindowState = wsNormal; finally TIniFile.Free; end; end; procedure TForm1.FormClose(Sender: TObject; var Action TCloseAction) var Ini: TIniFile; begin Ini := TIniFile.Create( ChangeFileExt( Application.ExeName, '.INI' ) ); try Ini.WriteInteger( 'Form', 'Top', Top); Ini.WriteInteger( 'Form', 'Left', Left); Ini.WriteString( 'Form', 'Caption', Caption ); Ini.WriteBool( 'Form', 'InitMax', WindowState = wsMaximized ); finally TIniFile.Free; end; end;
Each of the Read routines takes three parameters. The first parameter identifies the section of the ini file. The second parameter identifies the value you want to read, and the third is a default value in case the section or value doesn't exist in the ini file. Just as the Read methods gracefully handle the case when a section or value does not exist, the Write routines create the section and/or value if they do not exist. The example code creates an ini file the first time it is run that looks like this:
[Form]
Top=100
Left=100
Caption=Default Caption
InitMax=0
On subsequent execution of this application, the ini values are read in when the form is created and written back out in the OnClose event.
Don't be ungrateful.
When you ask something,and got the right answer,then at the bottom of the page of your question ,there is a writting : mark as solved ,so please, press that string to confirm as solved.Thank you ,in the name of the community...
Farewell...
When you ask something,and got the right answer,then at the bottom of the page of your question ,there is a writting : mark as solved ,so please, press that string to confirm as solved.Thank you ,in the name of the community...
Farewell...
![]() |
Similar Threads
- Move file pointer to read next file value (C++)
- read from file help (C++)
- write and read to file (C++)
- how to read a file from client side without browsing (ASP.NET)
- C++ How can read from file.txt & where can save this file(file.txt) to start reading (C++)
- create, write & read file to/from folder (C++)
- Read in a file and store in char array (C)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: Using a timer in Pascal?
- Next Thread: StringGrid doesn't redraw correctly
Views: 1001 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Pascal and Delphi
animation api app array button compile console data database dbisam delete delphi delphihelpimageforloop documentcomplete2 edit environment error errors events file form function gdi gis lasrautoinc media navigatecomplete2 network object open opengl pascal passing path player procedure search set sql table twebbrowser username variable win7 windows






