如何在 C++ 中使用 fstream 打开子文件夹
how to open subfolder with fstream in c++
你好,我想读取位于程序子文件夹中的文件。我成功地读取了程序主文件夹中的文件。但是我无法读取子文件夹中的文件。我尝试添加一个名为 subdir = "/*/" 的新字符串,并这样做来获取子文件夹,但程序拒绝使用我的字符串 subdir 采用新路径。有没有人知道如何做到这一点?我的想法快用完了,哈哈。
char resulta[ MAX_PATH];
DWORD flicksa = GetModuleFileName(NULL,resulta,MAX_PATH);
_chdir(std::string(resulta,GetCurrentDirectory(flicksa,resulta)).c_str());
std::string path = "/Users/Aymann/Desktop/arraysort/2CPP-REVISIONS/Supcount/";
std::string searchPattern = "/*.cpp";
std::string fullSearchPath = path + searchPattern;
WIN32_FIND_DATA FindData;
HANDLE hFind;
hFind = FindFirstFile( fullSearchPath.c_str(), &FindData );
if( hFind == INVALID_HANDLE_VALUE )
{
cout << "Error searching directoryn";
return -1;
}
do
{
char c;
string filePath = path + FindData.cFileName;
ifstream in( filePath.c_str() );
if( in.is_open() )
{in.get(c);
while (in) {
while (in && c != 'n') {
in.get(c);
}
_rowcount = _rowcount + 1;
in.get(c);
} // do stuff with the file here
}
else
{
cout << "Problem opening file " << FindData.cFileName << "n";
}
}
while( FindNextFile(hFind, &FindData) > 0 );
if( GetLastError() != ERROR_NO_MORE_FILES )
{
cout << "Something went wrong during searchingn";
}
不能将通配符传递给 fstream。
解决方案:下降文件夹,打开文件。
参见 glob(), opendir(), readdir(), closedir()。
相关文章:
- 如何将更多文件夹添加到c++include路径
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- VS2017,C++包含目录与附加包含目录,子文件夹包含失败-但为什么
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 如何在文档文件夹中创建目录
- "assimp/config.h"找不到,但它在文件夹中
- 无法打开 fstream C++文件,即使它与 .cpp 位于同一位置
- C++入门 5 版:类消息和文件夹
- 我可以在 Arduino 的库文件夹之外安装库吗?
- 如何按文件扩展名引用文件夹中的文件
- 在QTreeView中仅显示共享驱动器和文件夹
- 加载与引用 .NET DLL 位于同一文件夹中的引用的 .NET DLL 时"Not found"异常
- 介子复制/安装头文件到输出目录并保持文件夹结构
- vcruntime.h 从 Windows 包含文件夹中丢失?
- 在文件夹迭代上实现 RAII
- 使用 Powershell 命令将 cpp 文件的文件夹编译为 GNU 的 g++
- 使文件夹中的所有文件可供所有项目(子目录)访问
- 无法使用 CMake 从其他文件夹添加源文件
- 如何在安卓工作室中将c ++头文件从一个文件夹导入到另一个文件夹?
- 如何在 C++ 中使用 fstream 打开子文件夹