user3223395
user3223395

Reputation: 207

TCHAR* to char*

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

I want to convert "TCHAR f[MAX_PATH]" to const char * and pass to "Remove" function:

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();
}

Upvotes: 0

Views: 3912

Answers (2)

4pie0
4pie0

Reputation: 29724

If your project is unicode project TCHAR will be equivalent to a wchar_t rather than a char making your conversion attempts invalid.

As described here you need to use a function such as wcstombs when _UNICODE is defined. Either that or just use _tcslen (Look under Generic-Text Routine Mappings) on the TCHAR string and the compiler will transfer it to either strlen or wcslen depending if you are using unicode or not.

Upvotes: 0

Wojtek Surowka
Wojtek Surowka

Reputation: 20993

Use _tremove instead of remove. It works on const TCHAR*.

Upvotes: 2

Related Questions