0

This might be a newbie question, so I apologize in advance. I'm not very experienced in C. I have this code

#include <windows.h>
#include <locale.h>

void scanfiles(wchar_t *wstart_path)
{
    WIN32_FIND_DATA fdFile;
    HANDLE hFind = NULL;
    // file mask *.* to find everything
    size_t mlen = wcslen(wstart_path) + 5;
    wchar_t fsbuf[mlen];
    swprintf_s(fsbuf,mlen,L"%ls\\*.*",wstart_path);

    if((hFind = FindFirstFileW(fsbuf, &fdFile)) == INVALID_HANDLE_VALUE)
    {
        wprintf(L"Path not found: [%ls]\n", wstart_path);
        return;
    }
    size_t sbufsize = 256;
    wchar_t *s = (wchar_t*)malloc(sbufsize*sizeof(wchar_t));

    do
    {
        // FindFirstFile will return "."
        // and ".." as the first two directories
        if(wcscmp(fdFile.cFileName, L".") != 0
                && wcscmp(fdFile.cFileName, L"..") != 0)
        {
            mlen = wcslen(wstart_path) + wcslen(fdFile.cFileName) + 2;
            if (mlen > sbufsize) {
                s = (wchar_t*)calloc(mlen,sizeof(wchar_t));
                wmemset(s,L'\0',mlen);
            }
            else {
                wmemset(s,L'\0',sbufsize);
            }
            //Build up our file path using the passed in
            //wstart_path and the file/foldername we just found:
            wsprintf(s, L"%ls\\%ls\n", wstart_path, fdFile.cFileName);

            //Is the entity a File or Folder?
            if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
            {
            }
            else {
                wprintf(L"%s\n",s);
            }
        }
    } while(FindNextFileW(hFind, &fdFile)); //Find the next file.


    free(s);

    FindClose(hFind);
    return;
}

int main(int argc, char *argv[])
{
    setlocale(LC_CTYPE,"RUS");
    scanfiles(L"D:\\testdir");
    return 0;
}

And I have a file in D:\testdir with this name "比赛_исправлено.doc" My program gives me the following output:

D:\testdir\_исправлено.doc

What should I do to get the full name? How can I improve my program? What if I want not only print it but also write the path to a text file? Thanks.

  • 1
    try [`_setmode(_fileno(stdout), _O_U16TEXT);`](https://msdn.microsoft.com/en-us/library/tw4k6df8.aspx) but chances are your console font doesn't have these glyphs anyways... –  Jun 12 '17 at 06:57
  • You are right, it gives me D:\testdir\??_исправлено.doc – alexanderk409 Jun 12 '17 at 07:06
  • `??` means the absence of symbols in your font. – Zernike Jun 12 '17 at 07:12
  • I see. I'm able to write it to a txt file. Thanks. – alexanderk409 Jun 12 '17 at 07:30
  • Concerning this, the following link might be interesting: [SO: Unicode characters in Windows command line - how?](https://stackoverflow.com/questions/388490/unicode-characters-in-windows-command-line-how). I once tried something like this to apply to the console window of my Qt application (in Windows) but finally didn't get it running. (I didn't invest enough effort as the console is visible in debug mode only but closed in release mode.) – Scheff's Cat Jun 12 '17 at 07:33
  • The link I just posted contains another hint [SO: Output unicode strings in Windows console app](https://stackoverflow.com/questions/2492077/output-unicode-strings-in-windows-console-app) where actually just the hint of Felix Palmen is described. – Scheff's Cat Jun 12 '17 at 07:37

0 Answers0