Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Single Quote problem

Status
Not open for further replies.

Tilen

Programmer
Apr 2, 2005
75
SI
Hi all,

I hope someone can help me with this one.

Example:

I parse a text file (TStream) into an array.

Array:

Array = record
id:integer;
name:string;
lastname:string;
end;

The file is structured:

1 "Jack" "Moore"
2 "Mike" "Bold"
3 "Joe" "Cash"
...

I use TStrea.Read function to read a contetnt of a file, and then I parse the contect into Lines (TStringList) using :

ExtractStrings([#13],[' '],pchar(tekstfromfile),Lines);

which put into Lines each line from file int it's separate line. So Line[0]=1 "Jack" "More" , Line[1]=2 "Mike" "Bold" ...

It works OK, but the problem is if there is a signle quote ( ' )in name or lastname. Then it just messes up the whole data. It looks like it workd in a way that when it finds first single quote it looks for a closing single quote. And if it doesn't find it, it just assumes the whole rest of the text is as one string, instead of closed string with next ".

So if there's is a line 13 "Jack's" "Bold" ... it doesn't process the data correctly.

I tried all sort of things, but I just can't make it work.

Can someone give me some pointers what to do to tell Delphi to ignore single quote in my situation?

Thanx a lot guys.
 
the ' you use as the delimiter in the second parm is the single quote in code. For example to write "Welcome to Jack's place!" you would have to code:

writeln('Welcome to Jack''s place!');

I don't know if that's the answer you seek, but I'm 99% sure that you are telling it to use single quote as a parse parm from looking at the 2nd input you are giving it.
 

Have you looked at the .LoadFromFile method of TStringList?

 
Hi

I guess I didn't describe my problem clear enough.

The problem is, if single quote is in any of the text strings, Delphi seems to search for enclosing single quote and it disregards any #13 (new line) until it finds it.

But I found I could use AnsiReplaceStr function, to replace single quote with something else. But it's not that fast. I need to find a procedure that does it faster.

I can't use LoadFromFile, because it's a compressed stream.

So, basically what I need advice on, is where to find faster procedure than AnsiReplaceStr.

Any suggestions, appreciated.

Thanx a lot
 
Tilen,

I don't know if am on the right track but this code works?!

testfile 'test.text' contains :

1 "Jack" "Moore"
2 "Mike's" "Bold"
3 "Joe" "Cash"

just drop a TButton and a TMemo on a form and paste this code in the Button.Onclick event

Code:
 MyText:=TStringList.Create;
 MyText.LoadFromFile('c:\test.txt');
 Memo1.Lines.Assign(MyText);
 FreeAndNil(MyText);

what puzzles me is the fact you want to use TStream?
TFileStream contains the function ReadLn, so you can use that one to read the separate lines.

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
I didn't know about the readln for Tstream. I'll try it tomorrow, and let you know how it goes.

Thanx.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top