Solved

INI File readstring drops start and end quotes

Posted on 2006-06-27
4
454 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
ID: 16993623
Try
ands:=QuotedStr(ini.ReadString(checkname,'and words',''));
0
 
LVL 10

Expert Comment

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

Accepted Solution

by:
Russell Libby earned 250 total points
ID: 16994343

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
ID: 16999477
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

15 Experts available now in Live!

Get 1:1 Help Now