如何用c++将文件夹中的文件名存储到数组中
How to store name of file in folder to array in c ++?
我是c++的新手。我想写一个函数,从文件夹中取名字。
例如,我有一个名为C:\TEST的文件夹,在这个文件夹中,我有很多text.txt文件,我想将所有.txt文件名存储在字符串数组中。
任何人都可以在这个问题上帮助我。
我试过这样的东西,但失败了
const int arr_size = 10;
some_type src[arr_size];
// ...
some_type dest[arr_size];
std::copy(std::begin(src), std::end(src), std::begin(dest));
使用boost文件系统:
#include<vector>
#include<string>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <boost/filesystem.hpp>
using namespace std;
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
vector<string> fnames; //your filenames will be stored here
path p (argv[1]); // C:TEST
directory_iterator di(p);
directory_iterator di_end;
while(di != di_end)
{
fnames.push_back( di->path().filename().string() );
++di;
}
}
指定C:TEST
作为上面程序的命令行参数。
TCHAR szDir[MAX_PATH];
WIN32_FIND_DATA ffd;
HANDLE hFind = INVALID_HANDLE_VALUE;
hFind = FindFirstFile(szDir, &ffd);
if (INVALID_HANDLE_VALUE != hFind)
{
do
{
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// You can do a recursive search here, the current file is a directory
}
src[arr_size] = ffd.cFileName;
arr_size++;
}
while (FindNextFile(hFind, &ffd) != 0);
FindClose(hFind);
}
相关文章:
- 将字符串存储在c++中的稳定内存中
- 从命令行c++发送文本文件名
- std::原子加载和存储都需要吗
- C++:将控制台输出存储在宏中更好吗
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 使用QProcess执行命令,并将结果存储在QStringList中
- 访问存储在向量C++中的结构的多态成员
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 如何从存储在std::映射中的std::集中删除元素
- 存储模板类型以强制转换回派生<T>
- 类型总是使用其大小存储在内存中吗
- 当字符串存储在变量中时,如何将字符串转换为wchar_t
- 使用无符号字符数组有效存储内存
- 无法打开存储在字符串C++中的文件名
- 哪种数据类型用于存储文件名和文件大小的使用
- 在 std::string 变量中使用提升库存储文件名
- 7zip 字符串/流压缩 PHP/C?存档中没有文件名/日期存储.7z每个字节都很重要
- 如何用c++将文件夹中的文件名存储到数组中
- C++--文件名中的前导零(CSV文件)--存储/拆分std::矢量的值
- 你如何检查一个特定的文件名是否存在于谷歌云存储?