reading files from directory?

Started by
7 comments, last by nobodynews 16 years, 10 months ago
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);
                //AddImage(char*);
	}
 }



Advertisement
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.
anyway of doing in my current half assesd way.
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.

Also, what's the difference between AddSprites, and AddImage? Does AddImage do your actual file loading while AddSprites just collects the list of file names?

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
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.

Also, what's the difference between AddSprites, and AddImage? Does AddImage do your actual file loading while AddSprites just collects the list of file names?
This is correct.
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;    }  }}

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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

AddImage(char *path);
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

this i would add fullPath to my AddImage method
AddImage(fullPath);
}
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.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement