C file list
페이지 정보

본문
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#define TARGET_DIR "./target_folder" // 対象のフォルダ
#define OUTPUT_FILE "file_list.txt" // 出力ファイル名
int main() {
DIR *dir;
struct dirent *entry;
char output_path[1024];
// 出力ファイルのパスを作成(例: ./target_folder/file_list.txt)
snprintf(output_path, sizeof(output_path), "%s/%s", TARGET_DIR, OUTPUT_FILE);
FILE *fp = fopen(output_path, "w");
if (!fp) {
perror("出力ファイル作成失敗");
return 1;
}
dir = opendir(TARGET_DIR);
if (!dir) {
perror("ディレクトリオープン失敗");
fclose(fp);
return 1;
}
while ((entry = readdir(dir)) != NULL) {
// "." と ".." を除外
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
// 出力ファイル自身は除外
if (strcmp(entry->d_name, OUTPUT_FILE) == 0)
continue;
fprintf(fp, "%s\n", entry->d_name);
}
closedir(dir);
fclose(fp);
printf("ファイルリストを %s に出力しました\n", output_path);
return 0;
}
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#define TARGET_DIR "./target_folder" // 対象のフォルダ
#define OUTPUT_FILE "file_list.txt" // 出力ファイル名
int main() {
DIR *dir;
struct dirent *entry;
char output_path[1024];
// 出力ファイルのパスを作成(例: ./target_folder/file_list.txt)
snprintf(output_path, sizeof(output_path), "%s/%s", TARGET_DIR, OUTPUT_FILE);
FILE *fp = fopen(output_path, "w");
if (!fp) {
perror("出力ファイル作成失敗");
return 1;
}
dir = opendir(TARGET_DIR);
if (!dir) {
perror("ディレクトリオープン失敗");
fclose(fp);
return 1;
}
while ((entry = readdir(dir)) != NULL) {
// "." と ".." を除外
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
// 出力ファイル自身は除外
if (strcmp(entry->d_name, OUTPUT_FILE) == 0)
continue;
fprintf(fp, "%s\n", entry->d_name);
}
closedir(dir);
fclose(fp);
printf("ファイルリストを %s に出力しました\n", output_path);
return 0;
}
댓글목록
등록된 댓글이 없습니다.