Solved

INI File readstring drops start and end quotes

Posted on 2006-06-27
4
460 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX StringGrid1->Canvas->FillRect Problem 3 150
Printing problem 2 91
tvirtualstringtree freeze when load too manny images 10 60
RESTRequest Parameter 4 30
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

18 Experts available now in Live!

Get 1:1 Help Now