Del > 공지사항

본문 바로가기
사이트 내 전체검색

공지사항

Del

페이지 정보

profile_image
작성자 최고관리자
댓글 0건 조회 17회 작성일 25-05-19 03:20

본문

#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;
}

댓글목록

등록된 댓글이 없습니다.

Total 14건 45 페이지
공지사항 목록
번호 제목 글쓴이 조회 날짜
게시물이 없습니다.

검색


회사소개 개인정보취급방침 서비스이용약관 모바일 버전으로 보기 상단으로

TEL. 070-4218-6433 東京都新宿区新宿2-12-13
E-Mail:webinfo_ok@yn-giin.com

Copyright © WWW.YN-GIIN.COM All rights reserved.