如何使用c++列出Windows目录下的所有CSV文件
How to list all CSV files in a Windows Directory using C++?
我对c++有点陌生,我必须列出Windows目录下的所有CSV文件,我在谷歌上搜索了一下,发现了很多列出目录中所有文件的方法我想出了以下解决方案:
int listFiles(string addDir, vector<string> &list) {
DIR *dir = 0;
struct dirent *entrada = 0;
int isFile = 32768;
dir = opendir(addDir.c_str());
if (dir == 0) {
cerr << "Could not open the directory." << endl;
exit(1);
}
while (entrada = readdir(dir))
if (entrada->d_type == isFile)
{
list.push_back(entrada->d_name);
cout << entrada->d_name << endl;
}
closedir(dir);
return 0;
}
它正在使用Windows的direction .h(我使用VS2013),但问题是:—设置isFile = 32768是否正确?它会一直在Windows上运行吗?—如何判断是否为CSV文件?
另一件事,我试图使用windows.h/FindNextFile,但它没有工作。使用FindNextFile或上述解决方案更好吗?
我猜FindNextFile只列出CSV文件会更容易,但我不知道怎么做。
我的exit应该是一个字符串,因为它是一个读取CSV文件的函数的输入。
谢谢。
int listFiles(const string& addDir, vector<string> &list, const std::string& _ext) {
DIR *dir = 0;
struct dirent *entrada = 0;
int isFile = 32768;
std::string ext("." + _ext);
for (string::size_type i = 0; i < ext.length(); ++i)
ext[i] = tolower(ext[i]);
dir = opendir(addDir.c_str());
if (dir == 0) {
cerr << "Could not open the directory." << endl;
exit(1);
}
while (entrada = readdir(dir))
if (entrada->d_type == isFile)
{
const char *name = entrada->d_name;
size_t len = strlen(entrada->d_name);
if (len >= ext.length()) {
std::string fext(name + len - ext.length());
for (string::size_type i = 0; i < fext.length(); ++i)
fext[i] = tolower(fext[i]);
if (fext == ext) {
list.push_back(entrada->d_name);
cout << entrada->d_name << endl;
}
}
}
closedir(dir);
return 0;
}
int main()
{
vector<string> flist;
listFiles("c:\", flist, "csv");
system("PAUSE");
}
如果你想使用FindNextFile, msdn有一个例子来枚举目录中所有你可以适应的字段。
编辑:扩展windows API方法:
argv
的类型为TCHAR*
,根据#ifdef UNICODE
的不同,可以是char*
或wchar_t*
。它是所有Windows API调用使用的类型,它接受字符串参数。要创建TCHAR
字面值,可以使用TEXT("text")
。要创建wchar_t
字面值,可以使用L"text"
。如果不想使用TCHAR语义,可以将main
重新定义为int main(int argc, char* argv)
或int wmain(int argc, wchar_t* arv)
类型。在两种类型之间进行转换涉及到处理unicode和代码页,您可能应该使用第三方库。
将ASCII (std::string
或char*
, char点在0-127之间)转换为unicode(std::wstring
或wchar_t*
)只需创建一个std::wstring(std::string.cbegin(), std::string.cend())
即可。
下面是一个代码示例,演示如何使用WinAPI函数列出目录中的文件:
#include <windows.h>
#incldue <string>
#include <iostream>
#ifdef UNICODE
typedef std::wstring tstring;
#else
typedef std::string tstring;
#endif
#ifdef UNICODE
std::wostream& tcout = std::wcout;
std::wostream& tcerr = std::wcerr;
#else
std::ostream& tcout = std::cout;
std::ostream& tcerr = std::cerr;
#endif
int listFiles(const tstring& directory, std::vector<tstring> &list, const tstring& extension)
{
using std::endl;
WIN32_FIND_DATA file;
HANDLE hListing;
int error;
tstring query;
if (directory.length() > MAX_PATH - 2 - extension.length())
tcerr << "directory name too long" << endl;
query = directory + TEXT("*.") + extension;
hListing = FindFirstFile(query.c_str(), &file);
if (hListing == INVALID_HANDLE_VALUE) {
error = GetLastError();
if (error == ERROR_FILE_NOT_FOUND)
tcout << "no ." << extension << " files found in directory " << directory << endl;
return error;
}
do
{
if ((file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
{
tcout << file.cFileName << endl;
list.push_back(file.cFileName);
}
} while (FindNextFile(hListing, &file) != 0);
error = GetLastError();
if (error == ERROR_NO_MORE_FILES)
error = 0;
FindClose(hListing);
return error;
}
int _tmain(int argc, TCHAR* argv[])
{
std::vector<tstring> files;
listFiles(TEXT("C:\"), files, TEXT("sys"));
if (argc > 1)
listFiles(argv[1], files, TEXT("csv"));
}
如果你想简化它,你可以通过删除所有的T(TCHAR, TEXT(),新定义的tstring, tout, tcerr)变体并使用纯宽或非宽类型(如:char*,字符串,简单字面量,cout或wchar_t*, wstring, L"字面量,wcout)。如果你这样做,你需要使用WINAPI函数的特殊函数(即FindFirstFileA用于非宽和FindFirstFileW用于宽)
- 正在将csv文件读取为双精度矢量
- 如何选择在 csv 文件中输出的位置
- 有没有办法在不使用 getline() 的情况下从.csv文件中读取?
- 无法读取C++中的 CSV 文件
- 流:CSV 文件中的换行符
- 如何正确格式化csv文件?
- 正在分析CSV文件-C++
- 如何获取组件和搜索 CSV 文件
- C++ 中的 CSV 文件管理(特别是设置标头和每个标头将分别具有的值)
- 无法逐行读取.csv文件
- 非常快速地阅读 CSV 文件
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- CSV文件之间的差异,使用getline()产生不同的结果
- 将 CSV 文件中的双倍值C++到矢量中
- C++ 将 CSV 文件的列写入向量
- 使用getline读取.csv文件时出现问题
- CSV文件未从缓冲区获取数据
- 错误的值是使用c++从CSV文件数据写入数组
- 将csv文件加载到MySQL C++中
- 读取C++中的大型CSV文件(~4GB)