Del
페이지 정보
작성자 최고관리자 작성일 25-05-19 03:20 조회 21 댓글 0본문
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#define TARGET_DIR "./target" // 対象ディレクトリ
#define OUTPUT_FILE "./filelist.txt" // 出力ファイル
int main() {
DIR *dir;
struct dirent *entry;
FILE *fp;
dir = opendir(TARGET_DIR);
if (dir == NULL) {
perror("opendir");
return 1;
}
fp = fopen(OUTPUT_FILE, "w");
if (fp == NULL) {
perror("fopen");
closedir(dir);
return 1;
}
while ((entry = readdir(dir)) != NULL) {
// "."と".."をスキップ
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
fprintf(fp, "%s\n", entry->d_name);
}
fclose(fp);
closedir(dir);
// 出力ファイルサイズを確認
struct stat st;
if (stat(OUTPUT_FILE, &st) == 0) {
if (st.st_size > 0) {
if (remove(OUTPUT_FILE) == 0) {
printf("Output file '%s' deleted (size = %ld bytes).\n", OUTPUT_FILE, st.st_size);
} else {
perror("remove");
}
} else {
printf("Output file is empty, no deletion.\n");
}
} else {
perror("stat");
}
return 0;
}
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#define TARGET_DIR "./target" // 対象ディレクトリ
#define OUTPUT_FILE "./filelist.txt" // 出力ファイル
int main() {
DIR *dir;
struct dirent *entry;
FILE *fp;
dir = opendir(TARGET_DIR);
if (dir == NULL) {
perror("opendir");
return 1;
}
fp = fopen(OUTPUT_FILE, "w");
if (fp == NULL) {
perror("fopen");
closedir(dir);
return 1;
}
while ((entry = readdir(dir)) != NULL) {
// "."と".."をスキップ
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
fprintf(fp, "%s\n", entry->d_name);
}
fclose(fp);
closedir(dir);
// 出力ファイルサイズを確認
struct stat st;
if (stat(OUTPUT_FILE, &st) == 0) {
if (st.st_size > 0) {
if (remove(OUTPUT_FILE) == 0) {
printf("Output file '%s' deleted (size = %ld bytes).\n", OUTPUT_FILE, st.st_size);
} else {
perror("remove");
}
} else {
printf("Output file is empty, no deletion.\n");
}
} else {
perror("stat");
}
return 0;
}
댓글목록 0
등록된 댓글이 없습니다.