Skip to main content
GameDev.net gamedev.net
🔒 Locked

Finding files interface

Started by programering Jun 3, 2003 at 3:13 AM 3 replies 350+ views
Original Post
programering
programering
Hi guys i need some help! How can I find files in an certain file type(say .bmc) in a certain directory? Is it any such interface/header in MSVC++ include library, if it exists, what is it called(the header file)? I want to recieve an char array of existing file names in a directory. any help apprichiated! by the way does this work?: #define MAX_ARRAYS 10 typedef short[MAX_ARRAYS] array; Anton Karlsson Klingis Entertainment Games with silly humor
Qw3r7yU10p!
Qw3r7yU10p!
quote:
by the way does this work?:
#define MAX_ARRAYS 10
typedef short[MAX_ARRAYS] array;


no
Beer Hunter
Beer Hunter
<windows.h> has the functions "FindFirstFile", "FindNextFile", and "FindClose".

Example code:

#include <windows.h>

#include <string>
#include <vector>

...

std::vector<std::string> findFiles(const string& mask)
{
std::vector<std::string> result;

WIN32_FIND_DATA fileData;
HANDLE search = FindFirstFile(mask.c_str(), &fileData);
if (search == INVALID_HANDLE_VALUE) return result;

do
{
if ((fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
result.push_back(fileData.cFileName);
}
while (FindNextFile (search, &fileData));

FindClose(search);

return result;
}

...

std::vector<std::string> files = findFiles("c:/somewhere/*.txt");
For your other question, try: typedef short array[MAX_ARRAYS];

[edited by - Beer Hunter on June 3, 2003 4:40:32 AM]
zppz
zppz
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/findfirstfile.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/findnextfile.asp
programering
programering

Can I use a char **array; instead,
cos I want to code in just C.




void find_file_paths(char &**array, char *path)
{
WIN32_FIND_DATA fileData;
HANDLE search = FindFirstFile(path, &fileData);
if (search == INVALID_HANDLE_VALUE) return;
do {
if ((fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) strcpy(*array++, fileData.cFileName);
}
while (FindNextFile (search, &fileData)); FindClose(search);
}

char **array;

find_file_paths(array);



or
#define MAX_FILE_NAME_LENGTH 64
typedef char *FILE_NAME_ARRAY[MAX_FILE_NAME_LENGTH];

Anyway, I will use a struct made of myself called file_data.

struct file_data
{
short id;
short format;
char *path;
char *name;
char *extension;
};

Is string a typedef char *string; ?


Anton Karlsson
Klingis Entertainment
Games with silly humor

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.