如何在 c++ 中删除给定目录中的所有文本文件
How to delete all text files from given directory in c++
我正在尝试弄清楚如何从给定目录中删除所有文本文件。我正在使用Visual c ++ 2010 express,使用winapi。如果您知道文件的确切名称,我知道如何删除该文件,但我想删除该目录中的所有文本文件。这是我的最新尝试:
void deleteFiles( WCHAR file[] )
{
// WCHAR file[] is actually the directory path. i.e C:UsersTheUserDesktopFolder
// Convert from WCHAR to char for future functions
char filePath[ MAX_PATH ];
int i;
for( int i = 0; file[ i ] != ' '; i++ )
{
// Cycle through each character from the array and place it in the new array
filePath[ i ] = file[ i ];
}
// Place the null character at the end
filePath[ i ] = ' ';
// Generate WIN32_FIND_DATA struct and FindFirstFile()
WIN32_FIND_DATA fileData;
FindFirstFile( file, &fileData );
// Display the filename
MessageBox( NULL, fileData.cFileName, L"Check", MB_OK );
}
消息框仅显示所选文件夹,而不显示文件名。为什么会这样?
一个微妙的问题是你有两个具有相同名称和不同范围的变量:一个在循环外部定义,另一个未初始化;另一个在循环内声明。
我所指的变量是名为 i
的变量。
由于在循环外部定义的路径未初始化,因此当您将其用作终止路径的索引时,其值是不确定的,并且具有未定义的行为。
首先,将WCHARs
转换为char
不是一个好主意,因为路径可能包含 Unicode 字符,并且会出现错误。
第二件事是,要使FindFirstFile
工作,您需要添加通配符,例如 C:Path*
.
下面是 MSDN 上枚举目录中文件的示例:http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200%28v=vs.85%29.aspx
相关文章:
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何使用 c++ 实现并发文件/文本编辑?
- 我需要转换一些代码,以便它适用于输入和输出文件文本
- 通过套接字发送二进制文件.文本文件有效,其他文件无效
- 将大型数据文件拆分为多个小文件(文本格式)
- 如何读取输入文件(文本文件)并将输入验证为有效整数
- 从文本文件/文本流中读取Q字符串错误
- 从文本文件(文本解析器)C++中读取行和列的逻辑
- 如何将*.css文件(文本文件)的内容与附加信息存储在新文件中?
- 关于内存映射文件和使用大文件文本编辑器
- 如何在读取文件文本时跳过"n"
- 使用libzip从.zip获取文件(文本除外)