如何使用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; } int列表文件(字符串addDir、向量和列表){ DIR*DIR=0; 结构方向*entrada=0; int isFile=32768; dir=opendir(addDir.c_str()); if(dir==0){ cerr d_名称); 不能说出你的名字吗
如果您想简化它,您可以通过删除所有T(TCHAR,TEXT()、新定义的tstring、tcout、tcerr)变体并使用纯宽或非宽类型(即char*、string、simple literals、cout或wchar_T*、wstring、L“”literals、wcout),使您的应用程序锁定在unicode中或完全不使用unicode。如何使用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; } int列表文件(字符串addDir、向量和列表){ DIR*DIR=0; 结构方向*entrada=0; int isFile=32768; dir=opendir(addDir.c_str()); if(dir==0){ cerr d_名称); 不能说出你的名字吗,c++,windows,csv,directory-listing,C++,Windows,Csv,Directory Listing,如果您想简化它,您可以通过删除所有T(TCHAR,TEXT()、新定义的tstring、tcout、tcerr)变体并使用纯宽或非宽类型(即char*、string、simple literals、cout或wchar_T*、wstring、L“”literals、wcout),使您的应用程序锁定在unicode中或完全不使用unicode。 如果执行此操作,则需要使用WINAPI函数的特殊函数(即FindFirstFileA用于非广域,FindFirstFileW用于广域)如果你搜索一点,你可
如果执行此操作,则需要使用WINAPI函数的特殊函数(即FindFirstFileA用于非广域,FindFirstFileW用于广域)如果你搜索一点,你可能会找到。如果你不能让它工作,那么你应该发布一个关于该代码的问题。至于神奇数字
32768
是否正确,这取决于它,并且可能在不同平台之间有所不同。@Joachim Pileborg,我尝试过该代码,但它给了我一些错误,这很奇怪,因为我正在运行Windows a在MVS2013中进行nd编码。这就是我发布上述解决方案的原因,那么神奇的数字32768呢?我认为有更好的解决方案,但我不知道该怎么做。如果我使用它,我如何将字符串转换为参数argv[1](STRSAFE_PCNZWCH)?
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");
}
#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"));
}