0

I am using MFC CFindFIle for searching specific .dat file within the given search directory and I need to output the filepath and filename of the files that match the search criteria in the console (cout). It works, but returns strange results in console:

01E73500:000AB810:01E77558
01E77F40:000AB810:01E77FD0
01E740C8:01EA7478:01EA1D00
01EB78C8:01ECAA80:01EAF240
Press any key to continue . . .

Why is the output formatted in hex? Locale issues or something else? Code is running on win 7 64bit, en_us locale.

I am using the following code for searching

void Recurse(LPCTSTR pstr)
{
CFileFind finder;

// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.dat");

// start working for files
BOOL bWorking = finder.FindFile(strWildcard);

while (bWorking)
    {
    bWorking = finder.FindNextFile();

    // skip . and .. files; otherwise, we'd
    // recur infinitely!

    if (finder.IsDots())
        continue;

    // if it's a directory, recursively search it

    if (finder.IsDirectory())
        {
        CString str( finder.GetFileName());
        cout <<  str << endl;
        Recurse(str);
        }
    else {
        CString sFilePath(finder.GetFilePath());
            CString sFileName(finder.GetFileName());
        //CString sFileTitle(finder.GetFileTitle());

            cout << sFilePath << ":" << sFileName << ":" << endl;
        }
    }

finder.Close();
}
  • We'd need to see the definition of `operator<<(ostream &, CString)` or `CString::operator char *()` to decide. –  Nov 29 '13 at 20:27
  • 1
    Related, possible duplicate: [How can CString be passed to format string %s?](http://stackoverflow.com/q/6608942/464709) – Frédéric Hamidi Nov 29 '13 at 20:30

1 Answers1

2

Two issues: If the program is compiled to use unicode then use wcout instead of cout. CString is an object, not a wchar_t*, so the output you get is some hex address. Use:

wcout << (LPCTSTR)sFilePath;
ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15
  • You probably should implement [tcout](http://stackoverflow.com/q/5165160/464709) or cast to `LPCWSTR`, otherwise programs build in ANSI mode won't print properly. – Frédéric Hamidi Nov 29 '13 at 20:43
  • Instead of the cast you can use [`CString::GetString()`](http://msdn.microsoft.com/en-us/library/k59cayc6.aspx) as well. This returns a `const XCHAR*`, so Frédéric's comment is still valid. – IInspectable Nov 29 '13 at 22:37
  • wcout worked for me . Thanks. – Saman May 03 '16 at 23:33