我正在尝试使用 C 中的 Dirent 头文件读取目录。
我无法将文件名存储在 char 数组中的给定目录中。
代码如下。
char * FileNames;
while ( ( DirentPointer = readdir(DirectoryPointer) ) != NULL) {
strcpy(&FileNames[Counter], DirentPointer -> d_name);
Counter++;
}
当我运行应用程序时,我遇到了分段错误。我认为 strcpy 由于内存分配而导致应用程序出错。
谁能告诉我如何使用 malloc 和 realloc 将内存分配动态添加到 FileNames Char *?