首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TCHAR*改为char*

TCHAR*改为char*
EN

Stack Overflow用户
提问于 2014-01-22 12:52:23
回答 2查看 3.7K关注 0票数 0

Error : argument of type "TCHAR *" is incompatible with parameter of type "const char *" error in line : if(remove( f ) != 0 )

我希望将"TCHAR fMAX_PATH“转换为const char *,并将其传递到”删除“函数:

代码语言:javascript
复制
int _tmain(int argc, TCHAR *argv[])
{
    WIN32_FIND_DATA ffd;
    LARGE_INTEGER filesize;
    TCHAR szDir[MAX_PATH];
    size_t length_of_arg;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    DWORD dwError=0;
    TCHAR s[MAX_PATH];
    TCHAR f[MAX_PATH];

    // If the directory is not specified as a command-line argument,
    // print usage.

    if(argc != 2)
    {
        _tprintf(TEXT("\nUsage: %s <directory name>\n"), argv[0]);
        goto l;
    }

    // Check that the input path plus 2 is not longer than MAX_PATH.

    StringCchLength(argv[1], MAX_PATH, &length_of_arg);

    if (length_of_arg > (MAX_PATH - 2))
    {
        _tprintf(TEXT("\nDirectory path is too long.\n"));
        goto l;
    }

    _tprintf(TEXT("\nTarget directory is %s\n\n"), argv[1]);

    // Prepare string for use with FindFile functions.  First, copy the
    // string to a buffer, then append '\*' to the directory name.

    StringCchCopy(szDir, MAX_PATH, argv[1]);
    StringCchCopy(s, MAX_PATH, szDir);
    StringCchCat(szDir, MAX_PATH, TEXT("\\*"));

    // Find the first file in the directory.

    hFind = FindFirstFile(szDir, &ffd);

    if (INVALID_HANDLE_VALUE == hFind)
    {
        ErrorHandler(TEXT("FindFirstFile"));
        return dwError;
    }

    // List all the files in the directory with some info about them.
    StringCchCat(s, MAX_PATH, TEXT("/"));

    do
    {
        if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            _tprintf(TEXT("  %s   <DIR>\n"), ffd.cFileName);


        }
        else
        {
            StringCchCopy(f, MAX_PATH, s);
            StringCchCat(f, MAX_PATH, ffd.cFileName);

            filesize.LowPart = ffd.nFileSizeLow;
            filesize.HighPart = ffd.nFileSizeHigh;
            _tprintf(TEXT("  %s   %ld bytes\n"), ffd.cFileName, filesize.QuadPart);
            _tprintf(f);

            if(remove( f ) != 0 )
                perror( "Error deleting file" );
            else
                puts( "File successfully deleted" );

        }
    }
    while (FindNextFile(hFind, &ffd) != 0);

    dwError = GetLastError();
    if (dwError != ERROR_NO_MORE_FILES)
    {
        ErrorHandler(TEXT("FindFirstFile"));
    }

    FindClose(hFind);
    return dwError;

l:
    getch();
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-22 12:54:29

使用_tremove而不是remove。它在const TCHAR*上工作。

票数 2
EN

Stack Overflow用户

发布于 2014-01-22 13:01:40

如果您的项目是unicode项目,则TCHAR将等效于wchar_t而不是char,从而使您的转换尝试无效。

正如所描述的这里,在定义_UNICODE时,需要使用像[医]胃这样的函数。或者在TCHAR字符串上使用特斯林 (查看泛型文本例程映射),编译器将其传输到strlen或wcslen,这取决于您是否使用unicode。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21283495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档