Sign in to follow this  
Chett2001

Reading file names from a folder.

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
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[i] << std::endl;
}
return 0;
}

Share this post


Link to post
Share on other sites
WIN32_FIND_DATA FindData;
LPCTSTR myDir = "books/";

HANDLE hFind = FindFirstFile(myDir, &FindData);

if (hFind == INVALID_HANDLE_VALUE)
{
MessageBox(0, "Error...", "Error!", 0);
}
else
{
MessageBox(0, FindData.cFileName, Success!, 0);
}


Try this...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this