gpt4 book ai didi

c++ - 获取给定路径中的所有文件和子文件夹 - C/C++ 中的语言兼容性

转载 作者:行者123 更新时间:2023-11-30 17:29:45 25 4
gpt4 key购买 nike

所以基本上我需要列出所有文件和子文件夹以及其中的文件等,直到检查了所有路径,从每个对象读取数据并将其全部打包到一个文件中。

我不想使用 boost 或 minizip,因为对于如此简单的东西来说似乎太多了,所以我只是通过 dirent 自己编写它 - 一切正常我只是对标题中包含非 ANSI 字符的目录和文件有问题(我'我来自波兰)如 ąęóźż 等。我可以毫无问题地打开它们并从中读取所有数据,但是当我将它们的文件名打包到 std::string 时 - 它会损坏并且碰巧是例如:

TEST~1 而不是 TESTąę

如何将文件和目录打包到单个文件:

我通过 dirent 读取主目录的内容,如果我找到文件 - 我只是读取它的路径和数据到结构,如果我找到另一个目录 - 我只是读取它的结构路径并在该目录上递归调用搜索函数。

为了读取和写入文件,我使用 fopen、fwrite、fread。

请注意,文件内的数据写入没有问题 - 所有特殊字符都存储没有问题。

如有任何帮助,我们将不胜感激。

代码:

#include <list>
#include <string>
#include <dirent.h>
#include <cstdio>
#include <windows.h>

typedef struct {
std::string path;
unsigned lenght_of_path;
char type;
unsigned size;
char *data;
} single_element;



int pack(std::string root_directory, std::list<single_element> *all_elements)
//packs all files and subfolder and files and subfolder inside them etc into a std::list of structures
//please don't mind 0 error checks but I just want to get that language thing first
{
single_element temp;
temp.data = NULL;
FILE *processed;

DIR *directory;
struct dirent *ent;
if ((directory = opendir(root_directory.c_str())) != NULL)
{
while ((ent = readdir(directory)) != NULL)
{
std::string actual_path = root_directory;
std::string packed_file = ent->d_name;
if (packed_file != "." && packed_file != "..") //ignore .. and . in listing
{
if (ent->d_type == 16384) //if we found a directory
{
actual_path += "\\";
actual_path += packed_file;
temp.data = NULL;
temp.type = 'D'; // D is for directory, used later when extracting
temp.path = actual_path;
temp.lenght_of_path = actual_path.length();
temp.size = 0;
all_elements->push_back(temp);
pack(actual_path,all_elements); //recursively scan subfolder
}
else //we got file - I know there also can be other things as symbolic link but its only for test and I'm going to fix it once it works
{
actual_path += "\\";
actual_path += packed_file;
processed = fopen(actual_path.c_str(),"rb");
if (processed != NULL)
{
temp.path = actual_path;
temp.lenght_of_path = actual_path.length();
temp.type = 'F'; //F is for FILE;
fseek (processed,0,SEEK_END);
temp.size = ftell(processed);
all_elements->push_back(temp);
all_elements->back().data = new char[all_elements->back().size];
rewind(processed);
fread(all_elements->back().data,all_elements->back().size,1,processed);
fclose(processed);
}
}
}
}
}
closedir (directory);
return 0;
}

int save(std::string file_name, std::list<single_element> all_elements)
//write all_elements to single file
{
file_name += ".pak";
FILE *packed = fopen(file_name.c_str(),"wb");
int ammount = all_elements.size();
fwrite((const void*)&ammount,sizeof(int),1,packed); //fist write how many elements there are to be packed
for (std::list<single_element>::iterator i = all_elements.begin(); i != all_elements.end(); i++) //write each element
{
fwrite((const void*)&i->lenght_of_path,sizeof(unsigned),1,packed);
fwrite(i->path.c_str(),i->lenght_of_path,1,packed);
fwrite((const void*)&i->type,sizeof(char),1,packed);
if (i->type == 'F') //if its file write also data and it's size
{
fwrite((const void*)&i->size,sizeof(unsigned),1,packed);
fwrite(i->data,i->size,1,packed);
}

}
fclose(packed);
return 0;
}

int load(std::string file, std::list<single_element> *all_elements)
//load elements from file to list of structures
{
char *buf = NULL;
file += ".pak";
FILE *packed = fopen(file.c_str(),"rb");
single_element temp;
int ammount;
fread((void*)&ammount,sizeof(int),1,packed); //first read how many elements there are
for (int i = 0; i < ammount; i++)
{
fread((void*)&temp.lenght_of_path,sizeof(unsigned),1,packed);
buf = new char[temp.lenght_of_path+1];
fread(buf,temp.lenght_of_path,1,packed);
buf[temp.lenght_of_path] = '\0';
temp.path = buf;
delete buf;
buf = NULL;
fread((void*)&temp.type,sizeof(char),1,packed);
all_elements->push_back(temp);
if (temp.type == 'F') //if its file, also read its data and size
{
fread((void*)&all_elements->back().size,sizeof(unsigned),1,packed);
all_elements->back().data = new char[all_elements->back().size];
fread(all_elements->back().data,all_elements->back().size,1,packed);
}
else //else we got directory so just write 0 to size and NULL to data
{
all_elements->back().size = 0;
all_elements->back().data = NULL;
}
}
fclose(packed);
return 0;
}

int extract(std::string root_directory, std::list<single_element> all_elements)
{
CreateDirectory(root_directory.c_str(),NULL); //create root directory
std::list<single_element>::iterator i = all_elements.begin();
while (i != all_elements.end()) //first create all directories
{
if (i->type == 'D') //if we got a directory create it and erase from list
{
CreateDirectory(i->path.c_str(),NULL);
i = all_elements.erase(i);
}
else ++i;
}
i = all_elements.begin();
while (i != all_elements.end()) //now extract all files
{
FILE *extracted;
extracted = fopen(i->path.c_str(),"wb");
fwrite(i->data,i->size,1,extracted);
fclose(extracted);
++i;
}
return 0;

}

int main()
{
std::list<single_element> all;
std::string root = "Test";
pack(root,&all);
save(root,all);
load(root,&all);
extract(root,all);
return 0;
}

最佳答案

我有3条建议:

1)

如果你想遍历目录结构找到所有文件和目录并读取其内容,可以使用文件树遍历功能。

#include <stdio.h>
#include <ftw.h>

int callback_function(const char *name, const struct stat *s, int type)
{
// Here you can read the filename and contents of a file and save it in your own format.

printf("%s\n", name);
// type can have one of the following values and meaning:

// FTW_F File
// FTW_D Directory
// FTW_DNR Directory not readable
// FTW_SL Symbolic link
// FTW_NS The object is NOT a symbolic link and is one for which stat() could not be executed

return 0;
}

int main()
{
ftw(".", callback_function, 1);
return 0;
}

2)

使用 libtar http://www.feep.net/libtar/使用标准文件格式将文件连接成单个文件。

3)

依赖外部可执行文件,如 tar、zip、rar 等,并使用 system() 函数调用来调用它。

关于c++ - 获取给定路径中的所有文件和子文件夹 - C/C++ 中的语言兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25536379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com