Please help on how to read a file

Reply

Join Date: Apr 2009
Posts: 2
Reputation: 1shadow1 is an unknown quantity at this point 
Solved Threads: 0
1shadow1 1shadow1 is offline Offline
Newbie Poster

Please help on how to read a file

 
0
  #1
Apr 7th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2,034
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 225
tux4life's Avatar
tux4life tux4life is offline Offline
Postaholic

Re: Please help on how to read a file

 
0
  #2
Apr 7th, 2009
(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 can't build a reputation on what you are going to do."
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 531
Reputation: FlamingClaw will become famous soon enough FlamingClaw will become famous soon enough 
Solved Threads: 132
FlamingClaw's Avatar
FlamingClaw FlamingClaw is offline Offline
Posting Pro

Re: Please help on how to read a file

 
0
  #3
Apr 8th, 2009
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.
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...
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 531
Reputation: FlamingClaw will become famous soon enough FlamingClaw will become famous soon enough 
Solved Threads: 132
FlamingClaw's Avatar
FlamingClaw FlamingClaw is offline Offline
Posting Pro

Re: Please help on how to read a file

 
0
  #4
Apr 11th, 2009
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.
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3. Ini: TIniFile;
  4. begin
  5. Ini := TIniFile.Create( ChangeFileExt( Application.ExeName, '.INI' ) );
  6. try
  7. Top := Ini.ReadInteger( 'Form', 'Top', 100 );
  8. Left := Ini.ReadInteger( 'Form', 'Left', 100 );
  9. Caption := Ini.ReadString( 'Form', 'Caption', 'New Form' );
  10. if Ini.ReadBool( 'Form', 'InitMax', false ) then
  11. WindowState = wsMaximized
  12. else
  13.  
  14. WindowState = wsNormal;
  15. finally
  16. TIniFile.Free;
  17. end;
  18. end;
  19.  
  20. procedure TForm1.FormClose(Sender: TObject; var Action TCloseAction)
  21. var
  22. Ini: TIniFile;
  23. begin
  24. Ini := TIniFile.Create( ChangeFileExt( Application.ExeName, '.INI' ) );
  25. try
  26. Ini.WriteInteger( 'Form', 'Top', Top);
  27. Ini.WriteInteger( 'Form', 'Left', Left);
  28. Ini.WriteString( 'Form', 'Caption', Caption );
  29. Ini.WriteBool( 'Form', 'InitMax', WindowState = wsMaximized );
  30.  
  31. finally
  32. TIniFile.Free;
  33. end;
  34. 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...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1001 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Pascal and Delphi
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC