Board index » cppbuilder » FindFirst gives excption error

FindFirst gives excption error


2004-10-26 01:38:47 PM
cppbuilder15
Hi,
I have to following code:
TSearchRec srec;
TListItem *litem;
int result;
result = FindFirst("D:\\test\\*.dat", faAnyFile, srec); //<-this is the
line that
//generates the
exception
while(result == 0)
{
litem = ListView1->Items->Add();
litem->Caption = srec.Name;
litem->SubItems[0].Add(ExtractFileExt(srec.Name));
litem->SubItems[1].Add(srec.Size);
litem->SubItems[2].Add(srec.Time);
result = FindNext(srec);
}
FindClose(srec);
I have populate the D:\test\ directory with 2 .dat files, so it should add 2
entries to the TListView, but the code always genenrate an EAccessViolation
error at the line where the FindFirst is executed. Any idea what I've done
wrong? Any better suggestion to read/list files in a directory? (I've tried
with the Windows API version of FindFileFirst too, and the result is
simillar - it gives access violation in some random location). Thanks in
advance.
 
 

Re:FindFirst gives excption error

Okay, I've found out the real culprit is actually the line below, but I
followed the BCB help example for this, I still couldn't figure out what
goes wrong......
"Newsreader" < XXXX@XXXXX.COM >wrote in message
Quote
TListItem *litem;
 

Re:FindFirst gives excption error

Sorry, I've solve the problem with this code:
litem = ListView1->Items->Add();
litem->Caption = srec.Name;
litem->SubItems->Add(ExtractFileExt(srec.Name));
litem->SubItems->Add(srec.Size);
litem->SubItems->Add(srec.Time);
result = FindNext(srec);
Got confused by the TListItems and TListItem... Why the engineers at Borland
always like to make this kinda confusing redundancy? I've survived
TDBGrid->Columns, I've survived TListBox->Items->Strings, but I fall at
TListView->Items.....
However I'm still interested in knowing any better suggestions in listing
files in a directory with a TListView.
Regards.
"Newsreader" < XXXX@XXXXX.COM >wrote in message
Quote
Okay, I've found out the real culprit is actually the line below, but I
followed the BCB help example for this, I still couldn't figure out what
goes wrong......

"Newsreader" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>TListItem *litem;



 

{smallsort}

Re:FindFirst gives excption error

"Newsreader" < XXXX@XXXXX.COM >wrote in message news: XXXX@XXXXX.COM ...
Quote
Okay, I've found out the real culprit is actually the line below, but I
followed the BCB help example for this, I still couldn't figure out what
goes wrong......

"Newsreader" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>TListItem *litem;
That line is fine, it is not causing the problem.
Quote
litem->SubItems[0].Add(ExtractFileExt(srec.Name));
litem->SubItems[1].Add(srec.Size);
litem->SubItems[2].Add(srec.Time);
These line are the culprit. SubItems is a TStrings* and should be
used like this:
litem->SubItems->Add(ExtractFileExt(srec.Name));
litem->SubItems->Add(srec.Size);
litem->SubItems->Add(srec.Time);
Todd
 

Re:FindFirst gives excption error

"Newsreader" < XXXX@XXXXX.COM >wrote in message
Quote
result = FindFirst("D:\\test\\*.dat", faAnyFile, srec);
//<-this is the line that generates the exception
There is nothing in that code that can possibly cause any kind of exception
to be thrown at all.
Quote
litem->SubItems[0].Add(ExtractFileExt(srec.Name));
litem->SubItems[1].Add(srec.Size);
litem->SubItems[2].Add(srec.Time);
That is not valid code. The SubItems property does not have an '[]'
operator. That code should look like this instead:
litem->SubItems->Add(ExtractFileExt(srec.Name));
litem->SubItems->Add(srec.Size);
litem->SubItems->Add(srec.Time);
Quote
I have populate the D:\test\ directory with 2 .dat files, so it should
add 2 entries to the TListView, but the code always genenrate an
EAccessViolation error at the line where the FindFirst is executed.
What you describe is not possible. The AV has to be occuring somewhere
else.
Quote
I've tried with the Windows API version of FindFileFirst too, and
the result is simillar - it gives access violation in some random location
Please show the actual code.
Gambit