컴퓨터 엑셀 워드 포토샵 구글어스 WINDOWS JAVASCRIPT JAVA C++

 
Monday, December 25, 2006

C언어] 파일 타임 스탬프, 파일 날짜 시간 구하기 함수; File Time Stamp, Last Write Date


각 파일에는 "날짜/시간(Time of last modification of file)"이 있습니다. 타임 스탬프라고 하는데 그 날짜/시간을 구하는 방법입니다. 또한 타임 스탬프에는 다음과 같은 3종류가 있습니다:

* 만든 날짜 (Time of creation of file)

* 수정한 날짜 (Time of last modification of file)

* 액세스한 날짜 (Time of last access of file)


위의 3가지를 모두 구합니다.


VC++: '파일 처음 만든' '마지막 기록한' '마지막으로 접근한' 날짜/시간 구하기 예제


파일명: 0.cpp
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
#include <errno.h>

char* timeToString(struct tm *t);


int main(void) {
  struct _stat buf;
  char* filename = "Beatles, The - All My Loving.mp3";


  if ( _stat(filename, &buf) != 0 ) {
    switch (errno) {
      case ENOENT:
        fprintf(stderr, "File %s not found.\n", filename); break;
      case EINVAL:
        fprintf(stderr, "Invalid parameter to _stat.\n"); break;
      default:
        fprintf(stderr, "Unexpected error in _stat.\n");
    }
  }
  else {
    printf("%s\n", filename);
    printf( "\tTime Creation     : %s\n", timeToString(localtime(&buf.st_ctime)) );
    printf( "\tTime Last Written : %s\n", timeToString(localtime(&buf.st_mtime)) );
    printf( "\tTime Last Access  : %s\n", timeToString(localtime(&buf.st_atime)) );
  }


}




char* timeToString(struct tm *t) {
  static char s[20];

  sprintf(s, "%04d-%02d-%02d %02d:%02d:%02d",
              t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
              t->tm_hour, t->tm_min, t->tm_sec
          );

  return s;
}



컴파일 및 실행 결과 화면:
D:\Z>cl /nologo 0.cpp && 0.exe
0.cpp
Beatles, The - All My Loving.mp3
        Time Creation     : 2006-11-15 15:02:55
        Time Last Written : 2006-12-24 12:12:42
        Time Last Access  : 2006-12-25 12:01:23

D:\Z>


위의 경우 "Beatles, The - All My Loving.mp3"라는 파일의 타임 스탬프를 출력했습니다.



파일 날짜가 아닌, 시스템 날짜 구하기: ▶▶ C언어] 현재 날짜 시간, 년월일 시분초 로 출력 함수; Time To String Function VC++


파일 속성: ▶▶ C언어-VC] 파일 속성(읽기전용/히든 속성 등) 출력; Print File Attributes: ARHS


DIR 명령 구현 소스: ▶▶ C언어 VC] 도스 DIR 명령 구현; 파일 목록(리스트) 출력; 와일드 카드(Wild Card) 지원




tag: cpp
C언어 | C/C++ (Visual C++)

1 Comments:
At June 3, 2009 at 1:03 PM, Blogger Unknown said...

정말 감사합니다.

 

Post a Comment

<< Home RSS 2.0 feed

구글 Google 에서 제공하는 무료 블로그 서비스인 블로거 Blogger 의 인터넷 주소는 www.blogger.com 입니다. Blogger 에 블로그를 만들면, blogspot.com 이라는 주소에 블로그가 생성됩니다.
블로그를 직접 방문하지 않고도 최신 게시물을 구독하려면 RSS 2.0 feed 주소를 리더기에 등록하시면 됩니다.
Previous Posts
Monthly Archives
Top