鉴于这段代码,它是更大代码的一部分,但这是罪魁祸首。
在int main()
我这样做:
int main(int argc, char* argv[]) {
// code
// code
path2 = (char*)malloc(strlen(currentDir)*sizeof(char));
traverseDirectories(path2 , filename);
free(path2);
}
错误 :
a@ubuntu:~/Desktop$ ./exer4 fsdfsdfdafsd/ exer4
Could not open: No such file or directory
*** glibc detected *** ./exer4: free(): invalid next size (normal): 0x08b54038 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x6ebc2)[0x93ebc2]
/lib/i386-linux-gnu/libc.so.6(+0x6f862)[0x93f862]
/lib/i386-linux-gnu/libc.so.6(cfree+0x6d)[0x94294d]
/lib/i386-linux-gnu/libc.so.6(fclose+0x154)[0x92e0b4]
/lib/i386-linux-gnu/libc.so.6(perror+0xc5)[0x920a45]
./exer4[0x8048f52]
./exer4[0x8048b34]
./exer4[0x8048a99]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x8e9113]
./exer4[0x80487c1]
======= Memory map: ========
00398000-003b4000 r-xp 00000000 08:01 394171 /lib/i386-linux-gnu/libgcc_s.so.1
003b4000-003b5000 r--p 0001b000 08:01 394171 /lib/i386-linux-gnu/libgcc_s.so.1
003b5000-003b6000 rw-p 0001c000 08:01 394171 /lib/i386-linux-gnu/libgcc_s.so.1
008d0000-00a46000 r-xp 00000000 08:01 394150 /lib/i386-linux-gnu/libc-2.13.so
00a46000-00a48000 r--p 00176000 08:01 394150 /lib/i386-linux-gnu/libc-2.13.so
00a48000-00a49000 rw-p 00178000 08:01 394150 /lib/i386-linux-gnu/libc-2.13.so
00a49000-00a4c000 rw-p 00000000 00:00 0
00b5a000-00b5b000 r-xp 00000000 00:00 0 [vdso]
00c4b000-00c69000 r-xp 00000000 08:01 394137 /lib/i386-linux-gnu/ld-2.13.so
00c69000-00c6a000 r--p 0001d000 08:01 394137 /lib/i386-linux-gnu/ld-2.13.so
00c6a000-00c6b000 rw-p 0001e000 08:01 394137 /lib/i386-linux-gnu/ld-2.13.so
08048000-0804a000 r-xp 00000000 08:01 947365 /home/a/Desktop/exer4
0804a000-0804b000 r--p 00001000 08:01 947365 /home/a/Desktop/exer4
0804b000-0804c000 rw-p 00002000 08:01 947365 /home/a/Desktop/exer4
08b54000-08b75000 rw-p 00000000 00:00 0 [heap]
b7600000-b7621000 rw-p 00000000 00:00 0
b7621000-b7700000 ---p 00000000 00:00 0
b775f000-b7760000 rw-p 00000000 00:00 0
b776f000-b7771000 rw-p 00000000 00:00 0
bf87d000-bf89e000 rw-p 00000000 00:00 0 [stack]
Aborted
以下是方法:
int traverseDirectories(char * path , const char * filename)
{
// printf("\nCurrent path is :%s\n " , path);
struct dirent *direntp = NULL;
DIR *dirp = NULL;
size_t path_len;
char full_name[BUFFER];
strcpy(full_name, path);
displayAllFiles(full_name , filename); // display the file details to STDOUT
// code
// code
}
void displayAllFiles(char * directory , char * filename)
{
struct stat st;
DIR *dir;
struct dirent *ent;
char fullPathDirectory[BUFFER];
char fileDetails[BUFFER];
memset(fullPathDirectory, '\0', BUFFER); // reset the array of the input
memset(fileDetails, '\0', BUFFER); // reset the array of the input
dir = opendir (directory);
if (dir != NULL)
{
// print all the files and directories within directory
while ((ent = readdir (dir)) != NULL)
{
if (strcmp(ent->d_name , filename) == 0)
{
// printf("\nFile found!\n");
strcpy(fullPathDirectory , directory);
strcat(fullPathDirectory , "/");
strcat(fullPathDirectory , filename);
if (lstat(fullPathDirectory, &st) < 0)
{
perror(ent->d_name);
putchar('\n');
continue;
}
displayProperties(&st , fileDetails);
printf("\n%s" , fileDetails); // print file details
printf("%s\n" , fullPathDirectory); // print file path
}
}
closedir (dir);
}
else
{
// could not open directory
perror ("Could not open");
free(directory);
exit(EXIT_FAILURE);
}
}
我free
是directory
in 方法displayAllFiles
,但即使我在执行时收到此消息:
./exer4 fsdfsdfdafsd/ exer4
程序没有找到文件夹fsdfsdfdafsd
,这没问题,但为什么会打印所有的回溯?我想要它做的就是打印它no such file or directory
。
有任何想法吗 ?
谢谢