Public Group

This topic is 4059 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi what i am trying to do is basically have a function that takes an asset name , asset = directory to search and file name of file, reads the filename of the images and loads them. so i have this AddSprites(string asset) function and was wondering how do i read the file images starting with i.e stance 01.png, stance 01.png and so on. I need the resultant return be a char * to pass to my AddImage;
void AddSprites(string asset)
{
int iNumberOfFiles=0;

iNumberOfFiles = CountFiles("resources//"+asset+"//", "*", false);

string fullPath="resources//"+asset+"//";

char *p1 = new char[fullPath.size()+1];
strcpy (p1, fullPath.c_str());

if(iNumberOfFiles == -1){
wprintf("\nError no Files");
}
else
{
for(int i=0; i<iNumberOfFiles; i++)
{
strcat(p1, "i");//???
}

//wprintf("\nNumber of files found: %d", iNumberOfFiles);
wprintf("\nFileName: %s", p1);
}
}



##### Share on other sites
I would look into the boost filesystem library. They have a nice way to iterate through a directory and recurse down directory structures. The library has nice features for getting the filenames, directory names, etc.

##### Share on other sites
anyway of doing in my current half assesd way.

##### Share on other sites
There are multiple techniques available for getting directory info and loading images, but it depends on which OS you're using, and what technologies are available to you. So...

What OS is this targeted for?
Is this console, or GUI based?
If GUI - Which API are you using?

I'm sure someone will have an answer for you, but for me to provide you with a helpful answer requires I know the above info.

Cheers!

##### Share on other sites
What OS is this targeted for?
XP.

Is this console, or GUI based?
GUI.

If GUI - Which API are you using?
DirectX, MFC?

I'm sure someone will have an answer for you, but for me to provide you with a helpful answer requires I know the above info.

This is correct.

##### Share on other sites
For Windows you can use the FindFirstFile, FindNextFile and FindClose functions.

void EnumFilesInDirectory( const GR::tChar* szFindMask, std::list<GR::tString>& listFiles, bool bAllowDirectories, bool bClearList ){  WIN32_FIND_DATA   wFindData;  if ( bClearList )  {    listFiles.clear();  }  GR::tString     strDir = szFindMask;  size_t    iPos = strDir.find_last_of( '\\' );    if ( iPos == std::string::npos )  {    strDir.clear();  }  else  {    strDir = strDir.substr( 0, iPos + 1 );  }  if ( strDir.empty() )  {    GR::tChar    szTemp[MAX_PATH];    GetCurrentDirectory( MAX_PATH, szTemp );    strDir = szTemp;    if ( ( !strDir.empty() )    &&   ( strDir[strDir.length() - 1] != '\\' ) )    {      strDir += '\\';    }  }  if ( strDir.find_last_of( '\\' ) != std::string::npos )  {    // mit Backslash!    strDir = strDir.substr( 0, strDir.find_last_of( '\\' ) + 1 );  }  HANDLE    hFind = FindFirstFile( szFindMask, &wFindData );  if ( hFind == INVALID_HANDLE_VALUE )  {    return;  }  while ( hFind != INVALID_HANDLE_VALUE )  {    if ( wFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )    {      if ( ( bAllowDirectories )       &&   ( _tcscmp( wFindData.cFileName, _T( "." ) ) )      &&   ( _tcscmp( wFindData.cFileName, _T( ".." ) ) ) )      {        listFiles.push_back( strDir + wFindData.cFileName );      }    }    else    {      listFiles.push_back( strDir + wFindData.cFileName );    }    if ( !FindNextFile( hFind, &wFindData ) )    {      FindClose( hFind );      return;    }  }}

##### Share on other sites
i got a function that counts how many files are in the directory using a similar method but if you look at the code i wrote what i need is when the user inputs the asset name i.e stance it should auto matically looks for resources//srance//stance 01.png
resources//srance//stance 02.png

so how do i get the 01, 02 etc

becasue i need to pass it through my addImage method, which takes char* for the dir filepath i.e

##### Share on other sites
all i need is way to concatenate the path files so how would i go by doing this
i.e to get this oputput
resources//stance/stance 01.png
resources//stance/stance 02.png
resources//stance/stance 03.png
resources//stance/stance 04.png
resources//stance/stance 05.png
resources//stance/stance 06.png

The way i done it is i know how many files there are in a directory, so with that, what goes in here to get the above output?

for(int i=0; i<totalFiles; i++)
{
char fullPath[30];
need some sort of cat here to get this output

resources//stance/stance 01.png
resources//stance/stance 02.png
resources//stance/stance 03.png
resources//stance/stance 04.png

}

##### Share on other sites
As far as I can tell you know everything except how to add numbers to the end of a string, correct? Look into stringstreams, this will allow you to convert from an integer to a string. Then you can just concatatanate the number-string to the end of your file. here, I'll even give an example:

stringstream convert;
convert << my_integer;

to get a string from the stringstream object you can do either:

convert >> my_string;

or just use:

convert.str()

So your code may look something like this:
for(int i = 0; i < whatever; i++){  stringstream convert;  convert << i;  file = directory + slashie + part_of_file_name + space + convert.str() + extension;  fstream image_file(file.c_str(), flags);  // do stuff with image_file}

This code is completely untested and may contain serious errors. Hope it helps.

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
11
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631776
• Total Posts
3002304
×