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.