Solved

INI File readstring drops start and end quotes

Posted on 2006-06-27
4
469 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

696 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