Click here to Skip to main content
15,908,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i cannot seem to get rid of this error
TCHAR exts[] = { TEXT(".txt") ,TEXT(".docx") ,TEXT(".doc")}; /* null terminated array */


What I have tried:

TCHAR *exts;
exts = (TCHAR*)param;
//exts = param;

while (*exts) {
TCHAR *cur_ext;
cur_ext = PathFindExtension(file_path);
if (cur_ext) {
if (_tcscmp(cur_ext, exts) == 0) {
/* do what you want here with the file */
/* printf as an example here */
// _tprintf(TEXT("%s\n"), file_path);
//file << quoted(chk);
file << (TEXT("\n"))<
Posted
Updated 19-Feb-20 18:22pm
v4

This is a basic routine that works.

C++
BOOL rd_cb(LPTSTR lpszFile, PCTSTR pszExtension)
{
    PCTSTR cur_ext = PathFindExtension(lpszFile);
    if (cur_ext && _tcscmp(cur_ext, pszExtension) == 0)
    {
        wcout << "Found: " << lpszFile << endl;
        return TRUE;
    }
    
    return FALSE;
}

int RecursiveDirectory(PCTSTR lpszPath, PCTSTR pszExtension)
{
    WIN32_FIND_DATA    WFD;
    int                qwCount = 0;
    TCHAR              szFileSpec[MAX_PATH + 1];
    
    PathCombine(szFileSpec, lpszPath, TEXT("*.*"));
    HANDLE hSearch = FindFirstFile(szFileSpec, &WFD);
    if (hSearch == INVALID_HANDLE_VALUE) return 0;
    do
    {
        if (_tcscmp(WFD.cFileName, TEXT("..")) && _tcscmp(WFD.cFileName, TEXT("."))) 
        {
            if (WFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && !(WFD.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
            {
                PathCombine(szFileSpec, lpszPath, WFD.cFileName);
                qwCount += RecursiveDirectory(szFileSpec, pszExtension);
            }
            else 
            {
                PathCombine(szFileSpec, lpszPath, WFD.cFileName);
                if (rd_cb(szFileSpec, pszExtension))
                    qwCount++;
            }
        }
    } while (FindNextFile(hSearch, &WFD));
    FindClose(hSearch);
    return qwCount;
}


The initial call would be something like:
C++
RecursiveDirectory(TEXT("C:\\Users\\username\\Documents"), TEXT(".txt"));


[edit]
Replaced the lpCallback with a straightforward function call, to simplify the code.
[/edit]
 
Share this answer
 
v2
Comments
Member 12899279 15-Feb-20 10:59am    
argument of type "PCTSTR" is incompatible with parameter of type "LPVOID"
Richard MacCutchan 15-Feb-20 11:07am    
Well whatever you are doing you are not using the above code.
Member 12899279 15-Feb-20 11:10am    
i m using the exact above code i think you haven't seen all the code and haven't copile otherwise you would have known...
Richard MacCutchan 15-Feb-20 11:18am    
I have tested the above code exactly as shown. You must be doing something different which you are not showing me.
Stefan_Lang 17-Feb-20 5:08am    
Change the second function parameter in your typedef for the callback function from LPVOID to PCTSTR
Most likely it is because CHAR is not a defined type. Did you mean to use TCHAR?

[edit]
Try:
C++
TCHAR* exts[] = { TEXT(".txt") ,TEXT(".docx") ,TEXT(".doc")}; /* null terminated array of strings */

or better still
C++
PTCSTR exts[] = { TEXT(".txt") ,TEXT(".docx") ,TEXT(".doc")}; /* null terminated array */


[/edit]

[edit sequence="2"]
Something like ...
C++
// caller
PTCSTR exts[] = { TEXT(".txt") ,TEXT(".docx") ,TEXT(".doc"), NULL}; // null terminated array

DoSearch(TEXT("rootdirectory"), exts);
// ...

// function
... DoSearch(PCTSTR rootDirectory, PCTSTR* extensions)
{
    while (*extensions != NULL)
    {
        // process next extension
        Recursive(rootDirectory, *extensions);

        extensions++; point to the next entry
    }
}


[/edit]
 
Share this answer
 
v3
Comments
Member 12899279 15-Feb-20 7:17am    
ohh sorry my mistak yes it ws TCHAR i changed to CHAR whie trying to remove error
Member 12899279 15-Feb-20 7:31am    
i actually did tht but the problem is i am passing it to a funton and it aonly takes .txt extension and doesnt check other extensions here's sample of my func in which i am passing this
void ext_search(TCHAR *root_dir, TCHAR *exts)
{
RecursiveDirectory(root_dir, rd_cb, (void *)exts);
}
Richard MacCutchan 15-Feb-20 7:40am    
Once again you are only passing a pointer to the first entry: a pointer to an array of characters. Use PTSTR or PCTSTR which is an array of strings, and much clearer.

Also why are you using void* rather than the correct type?
Member 12899279 15-Feb-20 7:41am    
can you show how to do that?or can't i use loop to iterate through all extensions one by one?
Member 12899279 15-Feb-20 7:45am    
i just checked by replacing it with PTSTR but due to some limitations i can only use tchar so i need to do put some loop here to iterate through all of the extensions one by one and call the recursivedirectory()function with each extension one by one

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900