vivekpara
asked on
Return filenames to ListBox from array
I got some code from here and modified it to implement it myself in an MFC project I have. I can't seem to get it to return the file names to my listbox. What it seems to be doing is sending the address in memory of the pointer of the last file it looks at rather than returning the correct filenames. Any ideas?
void Readcfgini()
{
char **dirarray;
char *scandir;
scandir = "C:\\UI Code and Executable\\UIMFC\\UIMFC\\ Debug\\pro grams";
dirarray = 0;
dirarray = ScanDir( scandir , 0);
CString temp;
for(int i=0;i<sizeof(dirarray);i++ ){
/*(int k[sizeof(dirarray)],j;
for(j=0;j<sizeof(dirarray) ;j++)
{
k[j]=(int)dirarray[j];
//cout<<i[j]<<" "; //output the ascii chars
temp.Format("%d", k[j]);
}*/
temp.Format("%d", dirarray);
m_list.AddString(temp);
}
}
char ** ScanDir(char* dirname, int indent)
{
BOOL fFinished;
HANDLE hList;
TCHAR szDir[MAX_PATH+1];
WIN32_FIND_DATA FileData;
char ** names = NULL;
int count = 0 , maxnames = 0;
// Get the proper directory path
sprintf(szDir, "%s\\*", dirname);
// Get the first file
hList = FindFirstFile(szDir, &FileData);
if (hList == INVALID_HANDLE_VALUE)
{
return NULL; // No files, return empty list
}
else
{
// Traverse through the directory structure
fFinished = FALSE;
while (!fFinished)
{
// Make sure we have enough room in array to add another name
if (count >= maxnames)
names = (char **)realloc(names, (maxnames+=1024)*sizeof(ch ar*));
// Check the object is a directory or not
if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ((strcmp(FileData.cFileNam e, ".") != 0) &&
(strcmp(FileData.cFileName , "..") != 0))
{
names[count] = (char *)malloc(strlen(FileData.c FileName)+ 2);
strcpy(names[count], FileData.cFileName);
// add a trailing slash to indicate directory
strcat(names[count], "\\");
count++;
}
}
else
names[count++] = (FileData.cFileName);
if (!FindNextFile(hList, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
fFinished = TRUE;
}
}
}
}
return names;
}
void Readcfgini()
{
char **dirarray;
char *scandir;
scandir = "C:\\UI Code and Executable\\UIMFC\\UIMFC\\
dirarray = 0;
dirarray = ScanDir( scandir , 0);
CString temp;
for(int i=0;i<sizeof(dirarray);i++
/*(int k[sizeof(dirarray)],j;
for(j=0;j<sizeof(dirarray)
{
k[j]=(int)dirarray[j];
//cout<<i[j]<<" "; //output the ascii chars
temp.Format("%d", k[j]);
}*/
temp.Format("%d", dirarray);
m_list.AddString(temp);
}
}
char ** ScanDir(char* dirname, int indent)
{
BOOL fFinished;
HANDLE hList;
TCHAR szDir[MAX_PATH+1];
WIN32_FIND_DATA FileData;
char ** names = NULL;
int count = 0 , maxnames = 0;
// Get the proper directory path
sprintf(szDir, "%s\\*", dirname);
// Get the first file
hList = FindFirstFile(szDir, &FileData);
if (hList == INVALID_HANDLE_VALUE)
{
return NULL; // No files, return empty list
}
else
{
// Traverse through the directory structure
fFinished = FALSE;
while (!fFinished)
{
// Make sure we have enough room in array to add another name
if (count >= maxnames)
names = (char **)realloc(names, (maxnames+=1024)*sizeof(ch
// Check the object is a directory or not
if (FileData.dwFileAttributes
{
if ((strcmp(FileData.cFileNam
(strcmp(FileData.cFileName
{
names[count] = (char *)malloc(strlen(FileData.c
strcpy(names[count], FileData.cFileName);
// add a trailing slash to indicate directory
strcat(names[count], "\\");
count++;
}
}
else
names[count++] = (FileData.cFileName);
if (!FindNextFile(hList, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
fFinished = TRUE;
}
}
}
}
return names;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
BTW, http://www.codeproject.com/dialog/searchfolders.asp ("Browse Folder dialog, search folder and all sub folders using C/C++") seems to really cover your issue.
jkr has it right. You need an allocation for names[count] and a string copy. Instead of strdup you also could repeat the sequence you have in the if branch:
names[count] = (char *)malloc(strlen(FileData.c FileName)+ 1);
strcpy(names[count++], FileData.cFileName);
If you later want to change to C++ operator new (replacing malloc) and/or C++ containers like CStringList, CStringArray or std::vector<string>, you shouldn't forget to replace strdup as well, as strdup uses malloc.
Regards, Alex
names[count] = (char *)malloc(strlen(FileData.c
strcpy(names[count++], FileData.cFileName);
If you later want to change to C++ operator new (replacing malloc) and/or C++ containers like CStringList, CStringArray or std::vector<string>, you shouldn't forget to replace strdup as well, as strdup uses malloc.
Regards, Alex
ASKER
I'm trying it, but it seemed easier to recode than to use the old stuff now. Now how do I return file names in this new code, though? What I need is a way to store it in the stringarray and then be able to loop through my collection to read each of the files. I think my sizeof line will not work properly...but I'm not sure. Also, my knowledge of handles is useless...so if you have any suggestions on how to use them properly...please interject.
CStringArray astrPaths;
LPCTSTR name = "C:\\UI Code and Executable\\UIMFC\\UIMFC\\ Debug\\pro grams\\*.c fg";
WIN32_FIND_DATA data;
HANDLE fileHandle = FindFirstFile(name, &data);
for(int i=0;i<sizeof(fileHandle);i ++)
{
if(fileHandle == INVALID_HANDLE_VALUE) {
if(GetLastError() == ERROR_FILE_NOT_FOUND)
{
}
else
{
//astrPaths.Add((LPCTSTR)f ileHandle) ;
}
}
}
CStringArray astrPaths;
LPCTSTR name = "C:\\UI Code and Executable\\UIMFC\\UIMFC\\
WIN32_FIND_DATA data;
HANDLE fileHandle = FindFirstFile(name, &data);
for(int i=0;i<sizeof(fileHandle);i
{
if(fileHandle == INVALID_HANDLE_VALUE) {
if(GetLastError() == ERROR_FILE_NOT_FOUND)
{
}
else
{
//astrPaths.Add((LPCTSTR)f
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is what I ended up with:
void Readcfgini()
{
CStringArray astrPaths;
LPCTSTR name = "C:\\UI Code and Executable\\UIMFC\\UIMFC\\ Debug\\pro grams\\*.t xt";
WIN32_FIND_DATA data;
HANDLE fileHandle = FindFirstFile(name, &data);
while (fileHandle != INVALID_HANDLE_VALUE)
{
astrPaths.Add(data.cFileNa me);
m_list.AddString(data.cFil eName);
if (!FindNextFile(fileHandle, &data))
break;
}
DWORD err;
if((err = GetLastError()) != ERROR_NO_MORE_FILES)
{
return;
}
}
void Readcfgini()
{
CStringArray astrPaths;
LPCTSTR name = "C:\\UI Code and Executable\\UIMFC\\UIMFC\\
WIN32_FIND_DATA data;
HANDLE fileHandle = FindFirstFile(name, &data);
while (fileHandle != INVALID_HANDLE_VALUE)
{
astrPaths.Add(data.cFileNa
m_list.AddString(data.cFil
if (!FindNextFile(fileHandle,
break;
}
DWORD err;
if((err = GetLastError()) != ERROR_NO_MORE_FILES)
{
return;
}
}
ASKER
names[count++] = (FileData.cFileName);
Or it could be something in my Readcfgini function that is returning the data to the listbox.
Sigh!