Reading file names from a folder.

Started by
10 comments, last by Chett2001 16 years, 11 months ago
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.
Chett - "I look forward to helping all of you one day, but right now im just a noob learning his way through the perils of game Development."
Advertisement
Maybe you need to double-escape the \:
LPCTSTR myDir = "\\books\\*.ebr";
LPCTSTR myDir = "books\\*.ebr";


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


Chett - "I look forward to helping all of you one day, but right now im just a noob learning his way through the perils of game Development."
if (hFind == INVALID_HANDLE_VALUE) {    ::MessageBox(0, FindData.cFileName, 0, 0);} 

This is true when you don't find any matches. You should be checking for a valid handle.
VALID_HANDLE_VALUE doesnt exist, whats the name of it exactly?
Chett - "I look forward to helping all of you one day, but right now im just a noob learning his way through the perils of game Development."
Try and use the != instead of ==...
Allright done that, doesnt throw an error but still getting the same rubbish, is there anything else im doing wrong?
Chett - "I look forward to helping all of you one day, but right now im just a noob learning his way through the perils of game Development."
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;}
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?
Chett - "I look forward to helping all of you one day, but right now im just a noob learning his way through the perils of game Development."
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;}

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