TCHAR* to char*
TCHAR* to char*
Error : argument of type "TCHAR *" is incompatible with parameter of type "const char *"
error in line : if(remove( f ) != 0 )
我想将"TCHAR f[MAX_PATH]"转换为const char *
并传递给"删除"功能:
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 %snn"), 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 bytesn"), 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();
}
使用_tremove而不是删除。它适用于 const TCHAR*。
如果您的项目是 unicode 项目TCHAR
将等效于wchar_t
而不是char
,使您的转换尝试无效。
如此处所述,在定义_UNICODE时,您需要使用诸如 wcstombs 之类的函数。 要么这样做,要么只是在 TCHAR 字符串上使用 _tcslen(在泛型文本例程映射下查看),编译器会将其传输到 strlen 或 wcslen,具体取决于您是否使用 unicode。
相关文章:
- 如何解决"invalid conversion from 'char' to 'const char*'"
- Writing Char to Arduino EEPROM
- memcpy unsigned char to int
- 如何修复C++中的'cannot convert char to char*'错误?
- 返回"Cannot convert from 'std::ofstream {aka std::basic_ofstream<char>}' to bool"错误
- 使用strupr和strlwr时如何解决错误"invalid conversion of char to char*"?
- C++ cast char * to unsigned char
- Char to Int - C++
- Qt char* to QString
- 找不到错误"invalid conversion from char to const char*"的解决方案
- nameless char[] to std::string& reference
- atoi() - from char to int
- Unicode const char* to JString using JNI and C++
- 了解错误"error: cannot convert ‘char**’ to ‘const char*’ for argument ‘1’ to ‘double atof(const char*)’"
- const char* to LPCWSTR in Qt
- "incompatible types in assignment of 'char' to 'char'[100]"我使用的是 strcat 对吗?
- const char * to vector<unsigned char> Initalisation
- c++ const char* to char*
- 引起'Cannot convert from const char * to char *'
- char to char* issues