c - 制作一个列出目录中的文件并对输出进行排序的程序
问题描述
我opendir(.)
用来打开我要读取的目录的路径。然后我使用readdir
with 循环遍历当前目录中的所有文件和目录。然后我有一个char**
列表来存储 readdir 的内容。然后我进行排序**list
以按字典顺序排列。仅当我有一个长度超过 63 个字符的文件时,我才在对输出进行排序时不断收到溢出错误。为什么会这样,我该如何解决?
void ls_short() {
char **list = NULL;
int count = 0;
DIR *dir;
struct dirent *rd;
dir = opendir(".");
// check to make sure that we can open the directory
if(dir == NULL){
perror("Not able to open The current directory.");
exit(EXIT_FAILURE);
}
while((rd = readdir(dir)) != NULL){
list = realloc(list, (count+1)*sizeof(*list));
list[count] = rd ->d_name;
++count;
printf("%s\n",rd ->d_name);
}
printf("here we will print the list\n");
for (int i = 0; i < count;i++){
printf("%s\n",list[i]);
}
printf("Now we print the list in order and also get rid of . and .. directory\n");
list = realloc(list, (count+1)*sizeof(*list));
list[count] = "";
for (int i = 0; i < count - 1; i++){
for (int j = 0; j < count - i - 1; j++){
if (strcasecmp(list[j], list[j + 1]) > 0) {
char* temp;
temp = (char*)calloc(count, sizeof(char)*256);
strcpy(temp, list[j]);
strcpy(list[j], list[j + 1]);
strcpy(list[j + 1], temp);
free(temp);
}
}
}
for (int i = 2; i < count;i++){
printf("%s\n",list[i]);
}
closedir(dir);
}
这是我得到的错误:
=================================================================
==43335==ERROR: AddressSanitizer: strcpy-param-overlap: memory ranges [0x6250000001fd,0x625000000220) and [0x62500000021d, 0x625000000240) overlap
#0 0x103ef03ef in wrap_strcpy+0xaf (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x433ef)
#1 0x103e9aa5d in ls_short ls.c:61
#2 0x103e9a5c4 in main ls.c:16
#3 0x7fff20379620 in start+0x0 (libdyld.dylib:x86_64+0x15620)
0x6250000001fd is located 253 bytes inside of 8192-byte region [0x625000000100,0x625000002100)
allocated by thread T0 here:
#0 0x103ef54c0 in wrap_malloc+0xa0 (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x484c0)
#1 0x7fff2025b0fe in _readdir_unlocked$INODE64+0x6d (libsystem_c.dylib:x86_64+0x2a0fe)
#2 0x7fff2025b227 in readdir$INODE64+0x22 (libsystem_c.dylib:x86_64+0x2a227)
#3 0x103e9a6ad in ls_short ls.c:42
#4 0x103e9a5c4 in main ls.c:16
#5 0x7fff20379620 in start+0x0 (libdyld.dylib:x86_64+0x15620)
0x62500000021d is located 285 bytes inside of 8192-byte region [0x625000000100,0x625000002100)
allocated by thread T0 here:
#0 0x103ef54c0 in wrap_malloc+0xa0 (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x484c0)
#1 0x7fff2025b0fe in _readdir_unlocked$INODE64+0x6d (libsystem_c.dylib:x86_64+0x2a0fe)
#2 0x7fff2025b227 in readdir$INODE64+0x22 (libsystem_c.dylib:x86_64+0x2a227)
#3 0x103e9a6ad in ls_short ls.c:42
#4 0x103e9a5c4 in main ls.c:16
#5 0x7fff20379620 in start+0x0 (libdyld.dylib:x86_64+0x15620)
SUMMARY: AddressSanitizer: strcpy-param-overlap (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x433ef) in wrap_strcpy+0xaf
==43335==ABORTING
Abort trap: 6
解决方案
readdir()
有一个静态缓冲区rd->d_name
,每次调用它时都会重用它,因此所有list
元素都指向同一个字符串。
当您将字符串分配给数组时,您应该制作字符串的副本。
list[count] = strdup(rd->d_name);
并且在排序时,不要在字符串之间复制名称,只需交换指针即可。
if (strcasecmp(list[j], list[j + 1]) > 0) {
char* temp;
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
推荐阅读
- selenium-webdriver - 如何使用数据生成器运行 TestNG.xml
- r - 分隔具有不同长度的行值并删除其中一个值
- google-sheets - 谷歌表格查询计数返回“计数”而不是数值
- angular - 如何在实用程序类中注入 ng-bootstrap NgbDateParserFormatter
- dependency-injection - 无法在 webapi 控制器中使用 Unity.AspNet.Mvc 解决依赖关系
- css - 具有自适应宽度的网格状容器
- css - 如何在 CSS 动画中制作平滑曲线/旋转
- css - 如何使按钮水平排列?
- coq - 如何在 ltac 中使用类型参数?
- java - Spring boot如何测试WebServerInitialized事件