回答編集履歴
2
バグの報告。scandir を使わないコードの追加
answer
CHANGED
@@ -30,4 +30,35 @@
|
|
30
30
|
}
|
31
31
|
```
|
32
32
|
FILE *fp = fopen("xxx.csv", "w"); でファイルをオープンして、
|
33
|
-
printf(...); の代わりに、fprintf(fp, ...); にしてみてください。
|
33
|
+
printf(...); の代わりに、fprintf(fp, ...); にしてみてください。
|
34
|
+
|
35
|
+
**追記**
|
36
|
+
コードにバグがありました。free(entry[0]); が抜けています。
|
37
|
+
|
38
|
+
scandir の代わりに opendir/readdir/closedir を使っても書けます。
|
39
|
+
```C
|
40
|
+
#include <stdio.h> // printf
|
41
|
+
#include <stdlib.h> // free
|
42
|
+
#include <string.h> // strrchr, strcasecmp
|
43
|
+
#include <dirent.h> // opendir, readdir, closedir
|
44
|
+
|
45
|
+
int main(int argc, char *argv[])
|
46
|
+
{
|
47
|
+
if (argc != 2) { puts("argc"); return 1; }
|
48
|
+
|
49
|
+
struct dirent **entry;
|
50
|
+
DIR *dp = opendir(argv[1]);
|
51
|
+
if (!dp) { perror("opendir"); return 2; }
|
52
|
+
const char *sep = "";
|
53
|
+
struct dirent *ep;
|
54
|
+
while (ep = readdir(dp)) {
|
55
|
+
const char *p = strrchr(ep->d_name, '.');
|
56
|
+
if (p && !strcasecmp(p, ".ini")) {
|
57
|
+
printf("%s%s", sep, ep->d_name);
|
58
|
+
sep = ",";
|
59
|
+
}
|
60
|
+
}
|
61
|
+
printf("\n");
|
62
|
+
closedir(dp);
|
63
|
+
}
|
64
|
+
```
|
1
strlen -> strrchr
answer
CHANGED
@@ -2,15 +2,16 @@
|
|
2
2
|
```C
|
3
3
|
#include <stdio.h> // printf
|
4
4
|
#include <stdlib.h> // free
|
5
|
-
#include <string.h> //
|
5
|
+
#include <string.h> // strrchr, strcasecmp
|
6
6
|
#include <dirent.h> // scandir
|
7
7
|
|
8
|
-
int filter(const struct dirent *
|
8
|
+
int filter(const struct dirent *ep)
|
9
9
|
{
|
10
|
-
|
10
|
+
const char *p = strrchr(ep->d_name, '.');
|
11
|
-
return
|
11
|
+
return p && !strcasecmp(p, ".ini");
|
12
12
|
}
|
13
13
|
|
14
|
+
|
14
15
|
int main(int argc, char *argv[])
|
15
16
|
{
|
16
17
|
if (argc != 2) { puts("argc"); return 1; }
|