所以main()
应该提取当前目录中的所有文件名并将它们放入一个数组中,然后检查这些文件是否是常规文件。然后,它将所有常规文件传递到fun()
。
问题在于,即使main()
传入有效数组,数组的某些元素(并非所有元素)在传递到fun()
后都会被删除
//Output of command line before array is passed into fun()
reg 0: a.out
reg 1: myar
reg 2: proj2.tex
reg 3: myar.c
reg 4: ar
reg 5: makefile
reg 6: new.c
reg 7: newfile.txt
reg 8: newar
//Output of command line after passed into fun()
reg 0:
reg 1:
reg 2:
reg 3: myar.c
reg 4:
reg 5:
reg 6:
reg 7: newfile.txt
reg 8: newar
非常奇怪的是,如果我删除fopen()
中的fun()
语句,那么什么都不会出错。可能是fopen()
正在修改它不应该的东西吗?代码如下:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <dirent.h>
4 #include <sys/stat.h>
5
6 void fun(char* arfile, char** filenames, int file_count)
7 {
8 int i;
9 FILE* f = fopen("text.txt", "a");
10
11 for (i=0;i<file_count;i++){
12 printf("reg %d: %s\n",i,filenames[i]);
13 fflush(stdout);
14 }
15
16 fclose(f);
17 }
18
19
20 int main (int argc, char** argv)
21 {
22 struct stat s;
23 DIR *d;
24 struct dirent *dir;
25 int i, file_count = 0, reg_count = 0;
26 char **filenames, **regular_files;
27 char *arfile;
28
29 if (argc != 2)
30 exit(EXIT_FAILURE);
31 else
32 arfile = argv[1];
33
34 d = opendir(".");
35
36 while ((dir = readdir(d)) != NULL){
37 file_count++;
38 }
39
40 closedir(d);
41 filenames = malloc(file_count*sizeof(char*));
42 regular_files = malloc(file_count*sizeof(char*));
43
44 d = opendir(".");
45
46 for (i = 0; (dir = readdir(d)) != NULL; i++){
47 filenames[i] = dir->d_name;
48 }
49
50 closedir(d);
51
52 for (i = 2; i < file_count ; i++){
53 if (stat(filenames[i], &s) == -1) {
54 perror("Unable to read file stats");
55 exit(EXIT_FAILURE);
56 }
57
58 if (S_ISREG(s.st_mode)){
59 regular_files[reg_count] = filenames[i];
60 reg_count++;
61 }
62 }
63
64 for (i=0;i<reg_count;i++){
65 printf("reg %d: %s\n",i,regular_files[i]);
66 fflush(stdout);
67 }
68 fun(arfile, regular_files, reg_count);
69
70 free(filenames);
71 free(regular_files);
72 return 0;
73 }
74
答案 0 :(得分:1)
readdir
是不可重新输入的函数 - 它在每次迭代后保存临时静态缓冲区覆盖,因此dir->d_name
引用的数据变为无效。您需要复制它们,即使用strdup()
:
for (i = 0; (dir = readdir(d)) != NULL; i++) {
filenames[i] = strdup(dir->d_name);
}
不要忘记释放分配的字符串:
for (i = 0; i < file_count; i++) {
free(filenames[i]);
}
您还可以查看可重新输入的功能,例如readdir_r
,但它们对您的情况没有多大帮助。