Solved

INI File readstring drops start and end quotes

Posted on 2006-06-27
4
449 Views
Last Modified: 2010-04-05
Hello,

Working on a simple app that uses an inifile which contains search values within its ini strings.


In the ini file these look like this

               and words= 'Jim bob' 'sqirrel eater' 'big ol mammoth'
               or words= 'red jumper' 'big mistake'
the commands I use to read these are:
               ands:=ini.ReadString(checkname,'and words','');
               ors:=ini.ReadString(checkname,'or words','');

My problem is ands looks like
Jim bob' 'sqirrel eater' 'big ol mammoth
when I want it to look like
'Jim bob' 'sqirrel eater' 'big ol mammoth'

The start and end quotes get chopped off. The same happens for double quotes. For single word phrases, quotes may be excluded, so just sticking a quote on each end would cause my app to possibly take the entire string as a single search phrase.

If anyone can suggest a way round this without changing from quotes to something else like brackets I'd be very thankful.

All the best,
Kinnon_2000
0
Comment
Question by:kinnon_2000
  • 2
4 Comments
 
LVL 10

Expert Comment

by:atul_parmar
Comment Utility
Try
ands:=QuotedStr(ini.ReadString(checkname,'and words',''));
0
 
LVL 10

Expert Comment

by:atul_parmar
Comment Utility
Or
ands:='''' +ini.ReadString(checkname,'and words','') + '''';
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 250 total points
Comment Utility

Kinnon_2000,

Question for you; are you writing these values out to the ini yourself? If so, then you should enclose each string in dbl or single quotes when writing out to avoid the problem you are having. If you check the doc on GetProfileString, you will find the following:

---
Remarks

If the string associated with the lpKeyName parameter is enclosed in single or double quotation marks, the marks are discarded when the GetProfileString function returns the string.
---

which is why you are having the problem. If you have no control over this, then atul_parmar's comment would be one solution to use. If you do have control over writing the data out, then the following example demo's the writing of the data as well as the correct reading of it back in.

Regards,
Russell

---

const   QUO   =  #34;
begin

  with TIniFile.Create('c:\test.ini') do
  begin
     try
        WriteString('Testing', 'and words', QUO+'''Jim bob'' ''sqirrel eater'' ''big ol mammoth'''+QUO);
        WriteString('Testing', 'or words', QUO+'''red jumper'' ''big mistake'''+QUO);
        WriteString('Testing', 'word list', QUO+'one two three four five'+QUO);
        ShowMessage(ReadString('Testing', 'and words', EmptyStr));
        ShowMessage(ReadString('Testing', 'or words', EmptyStr));
        ShowMessage(ReadString('Testing', 'word list', EmptyStr));
     finally
        Free;
     end;
  end;

end;
0
 

Author Comment

by:kinnon_2000
Comment Utility
Many thanks for both your input. rllibby, in reply to your question about whether I'm writing the values or not, thats a yes. I think the double quotes idea is an acceptable solution. Eventually the values in the file will be dealt with using a nice setup application, but for now its all manual editing. I'm using an ini file rather than a database to keep things light. I fancied using an xml file but havn't used one witha delphi app before so skipped efficiency in place of speed. Maybe I'll change this later. Out of curiosity for future reference, do either of you folks know a good reference tutorial for working with xml using delphi? I havn't looked about yet but if you know one, that would be cool.

All the best,
Allan.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now