Solved

File question again

Posted on 2001-08-24
7
129 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

705 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