Solved

File question again

Posted on 2001-08-24
7
122 Views
Last Modified: 2010-04-06
I asked a question about how to lowercase all
strings between [ ] in a textfile ( all except [PRIMARY]

and now I have this:



var
 S: String;
 C: String;
 I: Integer;
 J: Integer;
 L: TStrings;
begin

 L := TStringList.Create;
 L.LoadFromFile('D:\000.TXT');
 S := L.Text;

 I := 1;
 while (I <= Length(S)) do
 begin
   while (I <= Length(S)) and (S[I] <> '[') do inc(I);
   inc(I);
   J := I;
   while (I <= Length(S)) and (S[I] <> ']') do inc(I);
   C := Copy(S, J, I - J);
   if C <> 'PRIMARY' then
   begin
     C := LowerCase(C);
     Delete(S, J, I - J);
     Insert(C, S, J);
   end;
 end;

 L.Text := S;
 L.SaveToFile('D:\000.TXT');
 L.Free;

end;


One problem is that the textfile i like to convert seems to be unicoded because wordpad cant read it and notepad can.

is there a way to modify the code abowe to have it handle unicoded files too?

0
Comment
Question by:hulken
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:ITugay
ID: 6422025
Hi hulken,
send me your file via e-mail to: igor@ns.kz
I will do something possible.
-----
Igor
0
 

Author Comment

by:hulken
ID: 6422042
Done!
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6422096
Hi hulken,

here is modifyed version:

var
  C: String;
  I: Integer;
  J: Integer;
  S: WideString;
  F: TFileStream;
begin
  F := TFileStream.Create('D:\000.SQL', fmOpenRead);
  SetLength(S, F.Size div 2);
  F.Read(S[1], F.Size);
  F.Free;

  I := 2;
  while (I <= Length(S)) do
  begin
    while (I <= Length(S)) and (S[I] <> '[') do inc(I);
    inc(I);
    J := I;
    while (I <= Length(S)) and (S[I] <> ']') do inc(I);
    C := Copy(S, J, I - J);
    if C <> 'PRIMARY' then
    begin
      C := LowerCase(C);
      Delete(S, J, I - J);
      Insert(C, S, J);
    end;
  end;

  F := TFileStream.Create('D:\001.SQL', fmCreate);
  F.Write(S[1], Length(S) * 2);
  F.Free;

end;
0
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.

 
LVL 9

Expert Comment

by:ITugay
ID: 6422101
Hi hulken,

replace
var
  C:String to C: WideString;
0
 

Author Comment

by:hulken
ID: 6427942
Thnks this works fine.

I just like to ask you 2 more things.

1.

Could you try to explain abit about the code. Then I might learn something.


2.

It would be nice if I could read the text into a Memo so I have a preview of it.
I can double the point's if you fix it.  Or should I add it as a new question?

0
 
LVL 9

Accepted Solution

by:
ITugay earned 200 total points
ID: 6427977
Hi,

here is code with some explanation and it allow you to show modified text into memo.

------
Igor.

PS: >>Or should I add it as a new question?
no, it is not necessary :-)

var
 C: WideString;
 I: Integer;
 J: Integer;
 S: WideString;
 F: TFileStream;
begin
 // open file for reading
 F := TFileStream.Create('D:\000.SQL', fmOpenRead);

 // every character in stream is two bytes length,
 // set length of string according file size div 2
 SetLength(S, F.Size div 2);

 // read file data into string
 F.Read(S[1], F.Size);

 // close file (free filestream)
 F.Free;

 //------------------------------
 // show source string in memo
 //------------------------------
 Memo2.Lines.Text := S;

 // first character is unicode identifier,
 // begin from second one
 I := 2;

 // do while not end of string
 while (I <= Length(S)) do
 begin
   // scan for '['
   while (I <= Length(S)) and (S[I] <> '[') do inc(I);
   inc(I);  // next character
   J := I;  // store position just after '['

   // scan for ']'
   while (I <= Length(S)) and (S[I] <> ']') do inc(I);

   // extract string between '[' and ']'
   C := Copy(S, J, I - J);

   // skip 'PRIMARY'
   if C <> 'PRIMARY' then
   begin
     // lowercase extracted string
     C := LowerCase(C);

     // delete last instance between '[' and ']'
     Delete(S, J, I - J);

     // insert new one in lowercase
     Insert(C, S, J);
   end;
 end;

 // open file for writing
 F := TFileStream.Create('D:\001.SQL', fmCreate);

 // every character of widestring is two bytes length
 // write string to the stream
 F.Write(S[1], Length(S) * 2);

 // close file (free stream)
 F.Free;

 //------------------------------
 // show modified string in memo
 //------------------------------
 Memo1.Lines.Text := S;
end;
0
 

Author Comment

by:hulken
ID: 6449867
Thnks!
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

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…
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…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

912 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