#include #include #include #include int directoryOpen (char* path); char *datFileOpen (char* path); int main (int argc, char **argv) { if (argc != 2) { printf("Missing argument.\n"); exit(1); } directoryOpen(argv[1]); return 0; } int directoryOpen (char* path) { DIR *dir; struct dirent *ent; char datpath[256]; char *line; // ディレクトリを開く if ((dir = opendir(path)) == NULL) { return 1; } // 読み終わるまで繰り返す while ((ent = readdir(dir)) != NULL) { if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { // ディレクトリリスト以外は抜ける continue; } strcpy(datpath, path); strncat(datpath, ent->d_name, strlen(ent->d_name)); // 1行目だけ読む line = datFileOpen(datpath); // ファイル名を出力 if (strcmp(line, "-ERR") == 0) { printf("%s Data Error\n", ent->d_name); continue; } else { printf("%s\n", ent->d_name); } free(line); } return 0; } char *datFileOpen (char* path) { // 変数定義 FILE *fp; char line[256]; char *ret; // 開けなかったら返す if ((fp = fopen(path, "r")) == NULL) { exit(1); } // 4byte 読み込み fgets(line, 5, fp); ret = malloc(strlen(line) + 1); strcpy(ret, line); // ポインタを返す return ret; }