Thread View: borland.public.cppbuilder.nativeapi
11 messages
11 total messages
Started by Safari
Fri, 28 Mar 2003 08:34
Problem in deleting file!
Author: Safari
Date: Fri, 28 Mar 2003 08:34
Date: Fri, 28 Mar 2003 08:34
25 lines
1565 bytes
1565 bytes
Hello to all,
I have problem, I can't delete files! Please help me.
bool __fastcall RemoveFile(char *hPath)
{
SHFILEOPSTRUCT hFile;
ZeroMemory(&hFile, sizeof(hFile));
hFile.hwnd = 0;
hFile.wFunc = FO_DELETE;
hFile.pFrom = hPath;
hFile.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI;
return SHFileOperation(&hFile);
}
void __fastcall TMain::BitBtn4Click(TObject *Sender)
{
for (int a = ListView->Items->Count - 1; a >= 0; a--)
{
RemoveFile((ListView->Items->Item[a]->Caption).c_str());
ListView->Items->Item[a]->Delete();
}
}
Thanks,
Safari.
Re: Problem in deleting file!
Author: Remy Lebeau (TeamB)
Date: Fri, 28 Mar 2003 09:08
Date: Fri, 28 Mar 2003 09:08
44 lines
2136 bytes
2136 bytes
"Safari" <Safari@alloymail.com> wrote in message
news:3E8408FF.5040308@alloymail.com...
> hFile.pFrom = hPath;
The pFrom and pTo members must be double-null terminated, but you're not
providing a second null terminator at all. Try this instead:
bool __fastcall RemoveFile(AnsiString Path)
{
Path += '\0';
SHFILEOPSTRUCT hFile = {0};
hFile.wFunc = FO_DELETE;
hFile.pFrom = Path.c_str();
hFile.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI;
return SHFileOperation(&hFile);
}
void __fastcall TMain::BitBtn4Click(TObject *Sender)
{
for (int a = ListView->Items->Count - 1; a >= 0; a--)
{
TListItem *Item = ListView->Items->Item[a];
RemoveFile(Item->Caption);
Item->Delete();
}
}
Or, just use DeleteFile() instead:
void __fastcall TMain::BitBtn4Click(TObject *Sender)
{
for (int a = ListView->Items->Count - 1; a >= 0; a--)
{
TListItem *Item = ListView->Items->Item[a];
DeleteFile(Item->Caption);
Item->Delete();
}
}
Gambit
Re: Problem in deleting file!
Author: Safari
Date: Fri, 28 Mar 2003 11:02
Date: Fri, 28 Mar 2003 11:02
52 lines
2469 bytes
2469 bytes
Thanks Mr. Dear Remy,
Can not i use "char *Path" and ".pFrom = Path"?
Safari
Remy Lebeau (TeamB) wrote:
> "Safari" <Safari@alloymail.com> wrote in message
> news:3E8408FF.5040308@alloymail.com...
>
>
>> hFile.pFrom = hPath;
>
>
> The pFrom and pTo members must be double-null terminated, but you're not
> providing a second null terminator at all. Try this instead:
>
> bool __fastcall RemoveFile(AnsiString Path)
> {
> Path += '\0';
>
> SHFILEOPSTRUCT hFile = {0};
> hFile.wFunc = FO_DELETE;
> hFile.pFrom = Path.c_str();
> hFile.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI;
> return SHFileOperation(&hFile);
> }
>
> void __fastcall TMain::BitBtn4Click(TObject *Sender)
> {
> for (int a = ListView->Items->Count - 1; a >= 0; a--)
> {
> TListItem *Item = ListView->Items->Item[a];
> RemoveFile(Item->Caption);
> Item->Delete();
> }
> }
>
> Or, just use DeleteFile() instead:
>
> void __fastcall TMain::BitBtn4Click(TObject *Sender)
> {
> for (int a = ListView->Items->Count - 1; a >= 0; a--)
> {
> TListItem *Item = ListView->Items->Item[a];
> DeleteFile(Item->Caption);
> Item->Delete();
> }
> }
>
>
> Gambit
>
>
Re: Problem in deleting file!
Author: Remy Lebeau (TeamB)
Date: Fri, 28 Mar 2003 18:26
Date: Fri, 28 Mar 2003 18:26
16 lines
1333 bytes
1333 bytes
Please clearify the question. Gambit "Safari" <Safari@alloymail.com> wrote in message news:3E842BD0.1060302@alloymail.com... > Can not i use "char *Path" and ".pFrom = Path"? --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.463 / Virus Database: 262 - Release Date: 3/17/03
Re: Problem in deleting file!
Author: nicolasr
Date: Fri, 28 Mar 2003 20:15
Date: Fri, 28 Mar 2003 20:15
82 lines
3086 bytes
3086 bytes
Hi,
if I understand correctly you want to pass the your path as a char*. But as
Remy told you the path needs a double \0 at the end. So either you pass a
char* to your RemoveFile that is already double \0 terminated or you copy it
inside the RemoveFile body to a local char array and add a second \0.
Sketch:
bool __fastcall RemoveFile(char *hPath)
{
char buffer[MAX_PATH+1];
strcpy(buffer, hPath);
buffer[strlen(hPath)+1] = '\0';
...
hFile.pFrom = buffer;
...
}
regards,
Nick
"Safari" <Safari@alloymail.com> escribi๏ฟฝ en el mensaje
news:3E842BD0.1060302@alloymail.com...
> Thanks Mr. Dear Remy,
> Can not i use "char *Path" and ".pFrom = Path"?
> Safari
>
> Remy Lebeau (TeamB) wrote:
> > "Safari" <Safari@alloymail.com> wrote in message
> > news:3E8408FF.5040308@alloymail.com...
> >
> >
> >> hFile.pFrom = hPath;
> >
> >
> > The pFrom and pTo members must be double-null terminated, but you're not
> > providing a second null terminator at all. Try this instead:
> >
> > bool __fastcall RemoveFile(AnsiString Path)
> > {
> > Path += '\0';
> >
> > SHFILEOPSTRUCT hFile = {0};
> > hFile.wFunc = FO_DELETE;
> > hFile.pFrom = Path.c_str();
> > hFile.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI;
> > return SHFileOperation(&hFile);
> > }
> >
> > void __fastcall TMain::BitBtn4Click(TObject *Sender)
> > {
> > for (int a = ListView->Items->Count - 1; a >= 0; a--)
> > {
> > TListItem *Item = ListView->Items->Item[a];
> > RemoveFile(Item->Caption);
> > Item->Delete();
> > }
> > }
> >
> > Or, just use DeleteFile() instead:
> >
> > void __fastcall TMain::BitBtn4Click(TObject *Sender)
> > {
> > for (int a = ListView->Items->Count - 1; a >= 0; a--)
> > {
> > TListItem *Item = ListView->Items->Item[a];
> > DeleteFile(Item->Caption);
> > Item->Delete();
> > }
> > }
> >
> >
> > Gambit
> >
> >
>
Re: Problem in deleting file!
Author: Remy Lebeau (TeamB)
Date: Fri, 28 Mar 2003 20:40
Date: Fri, 28 Mar 2003 20:40
30 lines
1710 bytes
1710 bytes
"nicolasr" <nicolasrNOSPAMATSIGNgmx.net> wrote in message
news:3e84ad5c$1@newsgroups.borland.com...
> char buffer[MAX_PATH+1];
You should allocate 2 extra spaces, not 1, in case the path is very long and
takes up the entire buffer:
char buffer[MAX_PATH+2] = {0};
> strcpy(buffer, hPath);
It's always safer to use strncpy() instead ot strcpy() to prevent possible
buffer overflows:
strncpy(buffer, hPath, MAX_PATH);
> buffer[strlen(hPath)+1] = '\0';
Not needed, see above.
Gambit
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.463 / Virus Database: 262 - Release Date: 3/17/03
Re: Problem in deleting file!
Author: nicolasr
Date: Sat, 29 Mar 2003 01:44
Date: Sat, 29 Mar 2003 01:44
14 lines
1449 bytes
1449 bytes
thanks for the hints.
Just a few questions to make sure I understand correctly:
Is
char buffer[MAX_PATH+2] = {0};
equivalent to
char buffer[MAX_PATH+2] = ""; ?
I have not seen ={0} yet.
I read up the strncpy() help. Is it here where the double \0 comes from? The
help says that strncpy() is null-padding the destination string.
thanks,
Nick
Re: Problem in deleting file!
Author: Remy Lebeau (TeamB)
Date: Sat, 29 Mar 2003 02:07
Date: Sat, 29 Mar 2003 02:07
37 lines
2689 bytes
2689 bytes
"nicolasr" <nicolasrNOSPAMATSIGNgmx.net> wrote in message
news:3e84fa8a@newsgroups.borland.com...
> Is
> char buffer[MAX_PATH+2] = {0};
> equivalent to
> char buffer[MAX_PATH+2] = ""; ?
{0} is a special-case shorthand that has an effect of zeroing out the entire
buffer's memory while initializing it. I just tested both cases, and they
both zeroed out the entire buffer's memory, so I guess functionally they're
the same as far as the generated code from the compiler is concerned. I
don't know what the C++ spec says the official rules are about initializing
fixed-sized character buffers with "", though. I always prefer to use {0}
because it can be applied to structures as well, which "" can't be.
> I read up the strncpy() help. Is it here where the
> double \0 comes from?
Not in this particular situation, no. strncpy() is simply a safer version
of strcpy() which allows you to specify the maximum number of characters to
copy, whereas strcpy() just copies until it reaches a null terminator
regardless of length. By creating the buffer with 2 extra characters and
then stopping the maximum copying just prior to them, I'm letting the
buffer's initialization ensure that there will always be at least 2 null
terminators in the buffer regardless of how many characters are actually
copied into the buffer later.
Gambit
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.463 / Virus Database: 262 - Release Date: 3/17/03
Re: Problem in deleting file!
Author: nicolasr
Date: Sat, 29 Mar 2003 02:11
Date: Sat, 29 Mar 2003 02:11
9 lines
1339 bytes
1339 bytes
thanks, now it's clear!
> {0} is a special-case shorthand that has an effect of zeroing out the
entire
> buffer's memory while initializing it.
this was new to me.
kind regards,
Nick
Re: Problem in deleting file!
Author: Early Ehlinger
Date: Mon, 31 Mar 2003 15:19
Date: Mon, 31 Mar 2003 15:19
24 lines
1979 bytes
1979 bytes
"Remy Lebeau (TeamB)" <gambit47@yahoo.com> wrote:
> I don't know what the C++ spec says the
> official rules are about initializing
> fixed-sized character buffers with "",
> though. I always prefer to use {0}
> because it can be applied to structures
> as well, which "" can't be.
I'm working from memory here, so I could be wrong - it would be a first for
me, of course :/
I'm pretty sure that the spec does /not/ require that the entire buffer be
zeroed out, so long as it starts with a zero-length null-terminated string
(i.e., starts w/ '\0'), so
char szBuf[5] = "";
could legally mean the same thing as:
char szBuf[5] = { '\0' , 'a' , 'b' , 'c' , 'd' };
--
-- Early Ehlinger CEO, ResPower Inc - Toll-Free : 866-737-7697
-- www.respower.com -- 500+ GHz Supercomputer Starting At USD$0.50/GHz*Hour
Re: Problem in deleting file!
Author: user
Date: Mon, 07 Apr 2003 04:48
Date: Mon, 07 Apr 2003 04:48
31 lines
1982 bytes
1982 bytes
Safari wrote:
> Hello to all,
> I have problem, I can't delete files! Please help me.
>
> bool __fastcall RemoveFile(char *hPath)
> {
> SHFILEOPSTRUCT hFile;
> ZeroMemory(&hFile, sizeof(hFile));
> hFile.hwnd = 0;
> hFile.wFunc = FO_DELETE;
> hFile.pFrom = hPath;
> hFile.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI;
> return SHFileOperation(&hFile);
> }
>
> void __fastcall TMain::BitBtn4Click(TObject *Sender)
> {
> for (int a = ListView->Items->Count - 1; a >= 0; a--)
> {
> RemoveFile((ListView->Items->Item[a]->Caption).c_str());
> ListView->Items->Item[a]->Delete();
> }
> }
>
> Thanks,
> Safari.
>
This isn't an answer to the question you asked, but if you're deleting a
single file and don't care to put it into the Recycle bin, you might
find the API function DeleteFile() a bit more straightforward.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads