LPCTSTR类型转换与GetFileAttributes一起使用 [英] LPCTSTR typecast for use with GetFileAttributes

查看:327
本文介绍了LPCTSTR类型转换与GetFileAttributes一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个方法来查看文件是否存在。
方法在它的当前是不完整的形式。
i'm试图想出为什么它不会作为代码。

I´m trying to build a method to see if a file exists. the method in it´s current isn´t complete form. i´m trying to figur out whyy it doesn´t behave as code.

    BOOL FileExists(LPCTSTR szPath)
        {
            //MessageBox(NULL,szPath,L"File Error",MB_OK);
          DWORD dwAttrib = GetFileAttributes(szPath);

            switch(dwAttrib)
            {

                case FILE_ATTRIBUTE_DIRECTORY:
                    MessageBox(NULL,L"FILE_ATTRIBUTE_DIRECTORY",L"File Error",MB_OK);
                    break;
                case FILE_ATTRIBUTE_ARCHIVE:
                    MessageBox(NULL,L"FILE_ATTRIBUTE_ARCHIVE",L"File Error",MB_OK);
                    break;
                case FILE_READ_ONLY_VOLUME:
                    MessageBox(NULL,L"FILE_READ_ONLY_VOLUME",L"File Error",MB_OK);
                    break;
                case FILE_INVALID_FILE_ID:
                    MessageBox(NULL,L"FILE_INVALID_FILE_ID",L"File Error",MB_OK);
                    break;
                default:
                    MessageBox(NULL,(LPCWSTR)dwAttrib,L"File Error",MB_OK);
                    break;
            }
return true;
        }

这是我如何调用方法:

FileExists((LPCWSTR)path.c_str());

这是我的文件所在的位置:

this is where my file resides:

std::string path = "C:\\smiley.bmp"


b $ b

我总是在这里结束,没有什么顺序切换的编码:

i always end up here, no mather in what order the switched is coded:

FILE_INVALID_FILE_ID

我想知道我的类型是否正确到LPCTSTR

i'm wondering if i'm typecastings correct to "LPCTSTR".

因为我试图使用MessageBox来显示szPath的内容,作为人类无尽的格式。

Because i tried using a MessageBox to display the contents of "szPath" and it showed up as human unreadeble format.

我提供的路径是正确的,正确转义。
如何检查szPath是否包含正确的信息?

the path i provide is correct, properly escaped. how can i check if "szPath" contains the proper information?

推荐答案

std :: string 是基于 char ,所以当你调用 .c_str(),您可以 char *

std::string is based on char, so when you call .c_str(), you can a char *.

LPCWSTR 是指向宽字符的指针。

But LPCWSTR is a pointer to a wide character string.

我相信,如果你正在编译与 UNICODE (因此 LPCTSTR == LPCWSTR 您需要使用 std :: wstring

I believe, if you're compiling with UNICODE enabled (so LPCTSTR == LPCWSTR), you'll need to use std::wstring.

相关:

  • Converting string to tchar in VC++

这篇关于LPCTSTR类型转换与GetFileAttributes一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆