Jump to content
  • Advertisement
Sign in to follow this  

Reading file names from a folder.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, ive been trying to read in the name of the first file in a certain folder but im not sure if the folder is relative or a full path.. AND im not sure on the syntax your supposed to use. here is my current code, hFind is calling an invalid handle value and it gives me a load of IIIII's with squigles over them.
	WIN32_FIND_DATA FindData; 

LPCTSTR myDir = "\books\*.ebr";
LPCTSTR foundFile ="";

     HANDLE hFind = FindFirstFile(myDir, &FindData); 
   
   foundFile = FindData.cFileName;
  
   if (hFind == INVALID_HANDLE_VALUE) 
  {
    ::MessageBox(0, FindData.cFileName, 0, 0);
  } 
So if anyone knows where im going wrong or can show me a proper example on how to do file paths in this situation i would be eternally grateful.

Share this post


Link to post
Share on other sites
Advertisement
LPCTSTR myDir = "books\\*.ebr";


doesnt throw an error but i stil cant get foundFile to show something understandable still "ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ" .


Share this post


Link to post
Share on other sites
This example will print out every file/subdirectory from the current directory.

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>

int main (int argc, char *argv[])
{
WIN32_FIND_DATA finddata;
memset (&finddata, 0, sizeof(finddata));

const LPCSTR pattern = "*.*";

HANDLE hmatch = FindFirstFile (pattern, &finddata);
if (hmatch != INVALID_HANDLE_VALUE) {
// Loop through every match
// (will output both files and directories)
do {
std::cout << finddata.cFileName << std::endl;
} while (FindNextFile (hmatch, &finddata));
} else {
std::cout << "No match for pattern '" << pattern << "'." << std::endl;
}

FindClose (hmatch);

return 0;
}

Share this post


Link to post
Share on other sites
The thing is i dont want to just write out the files in the directory, i want to place it into a LPCSTR/char*/string variable so i can choose one and using another function load the text in those files accordingly. So i need a way to save the name of a text file into a variable, and doing something like:


LPCSTR foundFile;
foundFile = finddata.cFileName;

doesnt work, got any idea of a solution?

Share this post


Link to post
Share on other sites
Well, look at the line where all of the printing occurs. Change the printing to something that will store that value into a string. I believe this:

std::string filename(finddata.cFileName);

will work, but I haven't tested it.

edit-I didn't see the part where you tried:

LPCSTR foundFile;
foundFile = finddata.cFileName;

edit2-What do you mean 'it didn't work'? Was there a compiler error? The following code Does work on my computer:

#define  WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
#include <string>
#include <vector>

int main ()
{
WIN32_FIND_DATA finddata;
memset (&finddata, 0, sizeof(finddata));

std::string pattern = "*.*";
std::vector<std::string> fileNames;
HANDLE hmatch = FindFirstFile (pattern.c_str(), &finddata);
if (hmatch != INVALID_HANDLE_VALUE) {
// Loop through every match
// (will output both files and directories)
do {
std::string fileName(finddata.cFileName);
fileNames.push_back(fileName);
} while (FindNextFile (hmatch, &finddata));
} else {
std::cout << "No match for pattern '" << pattern << "'." << std::endl;
}

FindClose (hmatch);

for(std::string::size_type i = 0; i < fileNames.size(); i++)
{
std::cout << fileNames << std::endl;
}
return 0;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!