Redirection of a std::list<CString> content to a text file

I am using that code to enumerate files with specific extensions in a drive and write them out to a text file.

std::ofstream file;
file.open("test.txt", std::ios::out | std::ios::app | std::ios::binary);

std::list<CString> listFiles;
FindFiles(_T("D:\\"), _T(".txt"), listFiles);
for (const auto& strFile : listFiles)
file << strFile.GetString() << std::endl;
return 0;

It outputs something like this in the text file. How can I write their address with their names instead of this garbage,
00000281BBACF338
00000281BBACEA78
00000281BBACF108
00000281BBAD1E48
00000281BBAD3D38
00000281BBAD3248
00000281BBAD22F8
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 #include <Windows.h>
#include <atlpath.h>
#include <list>
#include <iostream>
#include <fstream>

#ifdef _UNICODE
#define cout wcout
#endif

void FindFiles(
    const CString& strRootPath,
    const CString& strExt,
    std::list<CString>& listFiles,
    bool bRecursive = true)
{
    CString strFileToFind = strRootPath;
    ATLPath::Append(CStrBuf(strFileToFind, MAX_PATH), _T("*.*"));

    WIN32_FIND_DATA findData = { 0 };
    HANDLE hFileFind = ::FindFirstFile(strFileToFind, &findData);
    if (INVALID_HANDLE_VALUE != hFileFind)
    {
        do
        {
            CString strFileName = findData.cFileName;
            if ((strFileName == _T(".")) || (strFileName == _T("..")))
                continue;

            CString strFilePath = strRootPath;
            ATLPath::Append(CStrBuf(strFilePath, MAX_PATH), strFileName);
            if (bRecursive && (ATLPath::IsDirectory(strFilePath)))
            {
                FindFiles(strFilePath, strExt, listFiles);
            }
            else
            {
                CString strFoundExt = ATLPath::FindExtension(strFilePath);
                if (!strExt.CompareNoCase(strFoundExt))
                    listFiles.push_back(strFilePath);
            }

        } while (::FindNextFile(hFileFind, &findData));

        ::FindClose(hFileFind);
    }
}

int main()
{
    std::ofstream file;
    file.open("test.txt", std::ios::out | std::ios::app | std::ios::binary);

    std::list<CString> listFiles;
    FindFiles(_T("D:\\"), _T(".txt"), listFiles);
    for (const auto& strFile : listFiles)
        file << (LPCTSTR)strFile.GetString() << std::endl;
    return 0;
}

Thank you for replying.

//But it Still outputs this
0000017F883B31E8
0000017F883B4138
0000017F883B3648
0000017F883B3AA8
0000017F883B4598
Last edited on
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.
#ifdef _UNICODE
#define cout wcout
#endif
Right, so your 'CStrings' are the M$ abomination of wide strings then.

Read the links I posted to find out how to convert from that thing to std::wstring.

And you also need to open a wide output stream as well.
https://www.cplusplus.com/reference/ostream/wostream/

If your compiler supports C++17, you can avoid all that implementation specific cruft.
https://en.cppreference.com/w/cpp/filesystem
Registered users can post here. Sign in or register to post.