We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

C++ To Delphi Translation needed

GiantMatrix
GiantMatrix asked
on
Medium Priority
316 Views
Last Modified: 2010-04-05
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
Comment
Watch Question

Commented:
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;

Commented:
sorry a small bug, should be
    for i := 0 to nCount-1 do
Commented:
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;

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.