Mario.Tapilouw

Thursday, March 12, 2009

Obtaining filename in C++ Builder

Sometimes when we are working with image processing, we need to save the resulting image after finishing the process. Therefore we need the image file name so we can use the same file name with the original file with an additional prefix or suffix.

I write a small function for this purpose, the output is an AnsiString (string class of C++ Builder) and the result is a map with all the extracted information about the file such as drive, folder name, and file name.

Please remember to include map in your program

#include < map >


//---------------------------------------------------------------------------
std::map < int,AnsiString > TFormMain::parseFields( AnsiString filename, AnsiString separator )
{
std::map < int,AnsiString > lFields;
int index;
int i = 0;

while( 1 ) {
if( filename == "" )
break;

index = filename.AnsiPos(separator);

if( index == 0 ) {
lFields[ i ] = filename.SubString( 1, filename.Length() );
break;
}

lFields[i] = filename.SubString( 1, index - 1 );
filename = filename.SubString( index + separator.Length(), filename.Length() - index );
i++;
}

return lFields;
}
//---------------------------------------------------------------------------

when you want to use this function all you have to do is these steps:
1. create a map for the result
std::map< int,AnsiString > fileinfo
2. call the function:
fileinfo = parseFields(AnsiString filename, "\\");
3 get the field that you want
fileinfo[0, 1, ... fileinfo.size()-1]

Example output will be something like this:
filename : c:\image\wli\image1.bmp

the output will be:
fileinfo[0] : C:
fileinfo[1] : image
fileinfo[2] : wli
fileinfo[fileinfo.size()-1] : image1


That's it, any suggestions or comments?

1 Comments:

Post a Comment

<< Home