Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

INI File readstring drops start and end quotes

Posted on 2006-06-27
4
Medium Priority
?
478 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 1000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

636 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