gpt4 book ai didi

C 编程 - Strdup 未正确捕获文件名并将其存储在数组中

转载 作者:太空宇宙 更新时间:2023-11-04 01:19:01 25 4
gpt4 key购买 nike

我正在尝试将文件名存储在一个数组中。该数组在一个结构中,我想将在目录中找到的文件名存储在数组中。但是,我用来存储名称的过程似乎在此过程中损坏了 2 或 3 个名称。我认为问题出在 strdup 关键字上。每当我运行我的程序时,它要么读取程序可执行文件(位于我正在读取文件的目录上方的目录中),要么读取存储在前几个数组位置中的奇怪符号。以下是我试图捕获和存储文件名的程序的一部分,以及输出结果的图片:

typedef struct{
char *filename;
}filename;

typedef struct Configs{
char file_data_path[50];
char event_log_path[50];
filename *fn_data;
}Configs;

typedef struct TestConfigs{
bool done;
int selection;
int attempts_counter;
Configs tConfig;
}TestConfigs;

void read_files(struct TestConfigs *setup);

int main(void) {
printf("Hello Test\n");
TestConfigs setup;
read_files(&setup);
system("pause");
return EXIT_SUCCESS;
}

void read_files(struct TestConfigs *setup)
{
setup->done = false;
setup->attempts_counter = 3;

char cwd[1024];
DIR *dir = NULL;
struct dirent *pent = NULL;
struct stat info;
int total_num_of_files = 0;

strcpy(setup->tConfig.file_data_path, "data/");

chdir(setup->tConfig.frame_data_path);
if((getcwd(cwd, sizeof(cwd))) != NULL)
{
printf("Current Directory: %s\n", cwd);
}

dir = opendir(cwd);

if(dir != NULL)
{
while((pent = readdir(dir)) != NULL)
{
if(stat(pent->d_name, &info))
{
printf("ERROR: stat%s: %s\n", pent->d_name, strerror(errno));
}
else
{
if(S_ISREG(info.st_mode))
{
if((strcmp(pent->d_name, ".cproject") == 0) || (strcmp(pent->d_name, ".project") == 0))
{
continue;
}
else
{
total_num_of_files++;
}
}
}
}
printf("# of files found: %d\n", total_num_of_files);
rewinddir(dir);

// SETUP ARRAY HERE!
setup->tConfig.fn_data = malloc(total_num_of_files);
total_num_of_files= 0;

printf("During Storage Process:\n");
while((pent = readdir(dir)) != NULL)
{
if(stat(pent->d_name, &info))
{
printf("ERROR: stat%s: %s\n", pent->d_name, strerror(errno));
}
else
{
if(S_ISREG(info.st_mode))
{
if((strcmp(pent->d_name, ".cproject") == 0) || (strcmp(pent->d_name, ".project") == 0))
{
continue;
}
else
{
setup->tConfig.fn_data[total_num_of_files].filename = (char*)malloc(sizeof(pent->d_name));
setup->tConfig.fn_data[total_num_of_files].filename = strdup(pent->d_name); // <- Possible source of the storage problem
printf("Filename stored in fn_data[%d] = %s\n", total_num_of_files, setup->Config.fn_data[total_num_of_files].filename);
total_num_of_files++;
}
}
}
}

printf("\n");
printf("After Storage Process:\n");
for(int i = 0; i < total_num_of_files; i++)
{
printf("Filename stored in fn_data[%d] = %s\n", i, setup->tConfig.fn_data[i].filename);
}
}
closedir(dir);
}

Output results here

如何解决前几个数组位置中文件名的损坏存储问题?为什么只有前几个位置没有正确存储文件名,但其他位置都可以? strdup 是否存在问题?如果是,那么在数组中捕获和存储文件名的最佳替代方法是什么?提前致谢!

最佳答案

setup->tConfig.fn_data = malloc(total_num_of_files);

每个文件一个字节?

需要,例如,setup->tConfig.fn_data = malloc(total_num_of_files * sizeof(filename));

关于C 编程 - Strdup 未正确捕获文件名并将其存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49053436/

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