| View previous topic :: View next topic |
| Author |
Message |
b Guest
|
Posted: Mon Jul 28, 2003 8:46 pm Post subject: How can understand if my file has this extension? |
|
|
i open a file in this way:
if(SaveDialog1->Execute() == mrOk)
{
int const size = SaveDialog1->FileName.Length() + 2; // this 2 is for
an possible extension .c
char *filePath;
filePath = new char[size];
strcpy(filePath, SaveDialog1->FileName.c_str());
bool extension = false;
int l = 0;
while((!extension) && (l
{
if((filePath[l] == ".") && (filePath[l+1] == "c")) //<------ H E R
E
extension = true;
l++
}
//handle extension
i would understand if the file has a .c extension or not but using this code
i get an error: [C++ Error] Try.cpp(780): E2034 Cannot convert 'char' to
'char *'
|
|
| Back to top |
|
 |
JD Guest
|
Posted: Mon Jul 28, 2003 9:07 pm Post subject: Re: How can understand if my file has this extension? |
|
|
"b" <borland.public.cppbuilder.vcl.components.using> wrote:
if( SaveDialog1->Execute() )
{
String fExtension = ExtractFileExt( SaveDialog1->FileName );
if( fExtension.Length() > 0 )
{
// there is an extension
}
// or
if( fExtension == "c" || fExtension == "C" )
{
// file extention is 'c'
}
}
~ JD
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Jul 28, 2003 10:49 pm Post subject: Re: How can understand if my file has this extension? |
|
|
"b" <borland.public.cppbuilder.vcl.components.using> wrote
| Quote: | if(SaveDialog1->Execute() == mrOk)
|
Execute() returns a bool, not a TModalResult.
if( SaveDialog1->Execute() )
| Quote: | int const size = SaveDialog1->FileName.Length() + 2;
|
Under Windows95+, file extensions are allowed to be very long. You cannot
make any assumptions about the actual length of the extension for the chosen
file.
| Quote: | i would understand if the file has a .c extension or not
|
Rather then going through all of that hassle, why not just use the
ExtractFileExt() function?
if( SaveDialog1->Execute() )
{
AnsiString ext = ExtractFileExt(SaveDialog1->FileName);
if( AnsiSameText(ext, ".c") )
// do something
}
Gambit
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Jul 28, 2003 10:51 pm Post subject: Re: How can understand if my file has this extension? |
|
|
"JD" <nospam (AT) nospam (DOT) com> wrote
| Quote: | if( fExtension == "c" || fExtension == "C" )
{
// file extention is 'c'
}
|
ExtractFileExt() includes the period, so your checks above will always fail.
Also, you can use AnsiSameText() or AnsiCompareIC() to perform 1 check
instead of 2 separate ones.
Gambit
|
|
| Back to top |
|
 |
|