C++ To Delphi Translation needed

Hi All,

Can someone please translate the next block of code for me from C++ to Delphi:

//------------------------------------------------------------------------------------------------

   int  nCount;
   char MyString[500];
   TListItem* aLi1;
   AnsiString BadWordsFileName = ExtractFilePath(Application->ExeName) + "pkwhitelist.dat";

   
   FILE*   ptrf = fopen(BadWordsFileName.c_str(), "r");
   if (ptrf == NULL)
   {  return;
   }

   fscanf(ptrf, "%d\n", &nCount);

   if(nCount==0)
   { fclose(ptrf);  ListView1->Items->Clear();
     return;
   }

   ListView1->Items->Clear();

   for (INT i = 0; i < nCount; i++)
   {
          fscanf(ptrf, "%s\n", MyString);
          ListView1->Items->BeginUpdate(); // Efficiency

          aLi1 = ListView1->Items->Add();
          aLi1->Caption = MyString;
          ListView1->Items->EndUpdate();
   }

   fclose(ptrf);

//------------------------------------------------------------------------------------------------

Thanks in advance
GiantMatrixAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mokuleCommented:
This is translation with some improvements

var
  i: integer;
  nCount: integer;
  BadWordsFileName: string;
  f: textFile;
  MyString: string;
  aLi1: TListItem;
begin
  BadWordsFileName := ExtractFilePath(Application.ExeName) + 'pkwhitelist.dat';
  AssignFile(f,BadWordsFileName);
  try
    reset(f);
    readln(f,MyString);
    nCount := StrToIntDef(MyString,0);
    ListView1.Items.BeginUpdate; // better do it outside loop
    for i := 0 to nCount do
      begin
      aLi1 := ListView1.Items.Add;
      readln(f,MyString);
      aLi1.Caption := MyString;
      end;
    ListView1.Items.EndUpdate;
    CloseFile(f);
  except
  end;
end;
mokuleCommented:
sorry a small bug, should be
    for i := 0 to nCount-1 do
mokuleCommented:
I've missed also Clear Items

Once more whole code

var
  i: integer;
  nCount: integer;
  BadWordsFileName: string;
  f: textFile;
  MyString: string;
  aLi1: TListItem;
begin
  BadWordsFileName := ExtractFilePath(Application.ExeName) + 'pkwhitelist.dat';
  AssignFile(f,BadWordsFileName);
  try
    reset(f);
    readln(f,MyString);
    nCount := StrToIntDef(MyString,0);
    if nCount > 0 then
      ListView1.Items.Clear;
    ListView1.Items.BeginUpdate;
    for i := 0 to nCount-1 do
      begin
      aLi1 := ListView1.Items.Add;
      readln(f,MyString);
      aLi1.Caption := MyString;
      end;
    ListView1.Items.EndUpdate;
    CloseFile(f);
  except
  end;
end;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GiantMatrixAuthor Commented:
Hi mokule,

Can you help me with how to write to a file the content of Items in ListView1->Items?

If you can please let me know and I will open a new question for it in EE

Thanks in advance

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.