简体   繁体   English

C ++,FindFirstFile()的输出

[英]c++, output of FindFirstFile()

I wrote the program of finding file using FindFirstFile(...) function. 我编写了使用FindFirstFile(...)函数查找文件的程序。 But when I try to print the output of this function, there is a several strings of unknown characters printed in console windows. 但是,当我尝试打印此函数的输出时,在控制台窗口中打印了几串未知字符。 I read some posts, there was written to try using wcout instead of cout . 我读了一些帖子,写过尝试用wcout代替cout I try it, but it doesn't help. 我尝试了,但没有帮助。 I think, that the problem is in difference between ANSI and UNICODE encodings. 我认为,问题出在ANSI和UNICODE编码之间。 Can somebody help me? 有人可以帮我吗? I will be very thankful for any help! 我将非常感谢您的帮助!

Here is my code: 这是我的代码:

#include "FindFile.h"
#include <iostream>
using namespace std;

void FindFileCl::Execute(Input * input, Response * response )
{
    WIN32_FIND_DATAA FindFileData;

    HANDLE h = FindFirstFileA((input->FileName).c_str(),    // name of the file

        &FindFileData);
    if (h)
    {   



        cout << "Search Results:\n";

        cout<<(FindFileData.cFileName);


        CloseHandle(h);
    }
    else
    {
        cerr << "File is NOT found:" << GetLastError() << "\n";
    }




} 

If FindFirstFile() fails it returns INVALID_HANDLE_VALUE , not NULL : 如果FindFirstFile()失败,则返回INVALID_HANDLE_VALUE ,而不是NULL

If the function fails or fails to locate files from the search string in the lpFileName parameter, the return value is INVALID_HANDLE_VALUE and the contents of lpFindFileData are indeterminate. 如果函数失败或无法从lpFileName参数中的搜索字符串中找到文件,则返回值为INVALID_HANDLE_VALUE并且lpFindFileData的内容不确定。 To get extended error information, call the GetLastError function. 若要获取扩展的错误信息,请调用GetLastError函数。

and INVALID_HANDLE_VALUE is #define d as -1 (following macro located in WinBase.h ): INVALID_HANDLE_VALUE#define d为-1 (位于WINBASE.H下面的宏):

#define INVALID_HANDLE_VALUE ((HANDLE)(LONG_PTR)-1)

Meaning the if (h) will be entered in either success or failure. 表示将成功或失败输入if (h) In the event of failure, cFileName will not be modified resulting in junk being printed as it is not initialized. 如果失败,将不会修改cFileName ,因为未初始化它会打印出垃圾。 Change the if condition to explicitly check for INVALID_HANDLE_VALUE : 更改if条件以显式检查INVALID_HANDLE_VALUE

if (h != INVALID_HANDLE_VALUE)
{
}

One of the "least bad" ways would be to convert the Unicode name to the Console's encoding. 一种“最糟糕的”方法是将Unicode名称转换为控制台的编码。

For this, I suggest compiling in Unicode (There's a project option for that in Visual Studio >=8; otherwise you have to define both UNICODE and _UNICODE manually), using the TCHAR version of FindFirstFile(), and then using CharToOem() or CharToOemBuff() (neither is perfect). 为此,我建议使用Unicode(在Visual Studio中有一个项目选项> = 8;否则,您必须手动定义UNICODE_UNICODE ),使用TCHAR版本的FindFirstFile(),然后使用CharToOem()或CharToOemBuff() (都不是完美的)。 -- Or alternately, use the W version followed by WideCharToMultiByte(CP_OEMCP). -或者,使用W版本,后跟WideCharToMultiByte(CP_OEMCP)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM