簡體   English   中英

獲取最后創建(修改)的文件

[英]Getting the last created (modified) file

我有一個程序可以創建文件並用數據填充它們,這沒關系,它是根據實際時間(YYYYMMDDHHMMSS)命名的。 現在,我想始終打開最后創建的文件,即最近的文件,這在C中可能嗎? 如果是的話,我會很感激嗎?

UPDATE

我需要說清楚。

說我有一個想要使用的字符串,例如:

..............
FILE* input = NULL;
char* fileName = NULL;
...............// in some getting the name of the last modified file 
   and than open it 
inp = fopen(fileName,"rb");

ftw()函數在這里可能很有用。 它將為目錄中的每個文件和目錄調用一個函數(您需要編寫)。 您的函數將確定其參數是否比以前看到的參數新,如果有,則將其記錄在全局變量中。

一個警告是ftw將查看每個子目錄中的每個文件。 那可能不是您想要的。 但是如果可以的話,使用ftw將使您的代碼更簡潔,因為它可以為您進行目錄掃描和統計。 這是我編寫的一個示例,該示例將在當前目錄中找到最新修改的文​​件:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <ftw.h>

char newest[PATH_MAX];
time_t mtime = 0;

int checkifnewer(const char *path, const struct stat *sb, int typeflag)
{
    if (typeflag == FTW_F && sb->st_mtime > mtime) {
        mtime = sb->st_mtime;
        strncpy(newest, path, PATH_MAX);
    }
    return 0;
}

main()
{
    ftw(".", checkifnewer, 1); 
    printf("%s\n", newest);
}

這可以通過使用stat函數來完成。

使用stat()函數可以獲得文件信息。 stat結構包含-

struct stat {
               dev_t     st_dev;     /* ID of device containing file */
               ino_t     st_ino;     /* inode number */
               mode_t    st_mode;    /* protection */
               nlink_t   st_nlink;   /* number of hard links */
               uid_t     st_uid;     /* user ID of owner */
               gid_t     st_gid;     /* group ID of owner */
               dev_t     st_rdev;    /* device ID (if special file) */
               off_t     st_size;    /* total size, in bytes */
               blksize_t st_blksize; /* blocksize for file system I/O */
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
               time_t    st_atime;   /* time of last access */
               time_t    st_mtime;   /* time of last modification */
               time_t    st_ctime;   /* time of last status change */
           };

檢查其他文件的每個文件時間信息,該文件具有最高的時間詳細信息,將其存儲到另一個stat結構中,並比較所有文件。 並最終打開您在stat結構中存儲的文件!

您無需提取文件的時間戳即可檢查最后創建的文件(這很昂貴),而無需使用inotify() ,請參見此處 每當創建新文件時, inotify()都會告訴您。

掃描目錄,並記住帶有最大時間戳的文件。 如果文件名遵循YYYYMMDDHHMMSS格式,則足以找到“最大”文件名的文件,如下所示:

char buffer[MAX_LEN];
void recentByName(const char* path, char* recent){
  DIR* dir  = opendir(path);
  struct dirent* entry;
  recent[0] = '\0';
  while (NULL != (entry = readdir(dir))) {
    if (!isExceptionalDir(entry->d_name)) {
      if (strncmp(recent, entry->d_name, MAX_LEN)<0) {
        strncpy(recent, entry->d_name, MAX_LEN);
      }
    }
  }
  closedir(dir);
}

但是,3.stat()在給出實際修改時間時可能更可靠,然后嘗試執行以下操作:

void recentByModification(const char* path, char* recent){
  struct dirent* entry;
  time_t recenttime = 0;
  struct stat statbuf;
  DIR* dir  = opendir(path);
  while (NULL != (entry = readdir(dir))) {
    if (!isExceptionalDir(entry->d_name)) {
      sprintf(buffer, "%s/%s", path, entry->d_name);
      stat(buffer, &statbuf);
      if (statbuf.st_mtime > recenttime) {
        strncpy(recent, entry->d_name, MAX_LEN);
        recenttime = statbuf.st_mtime;
      }
    }
  }
  closedir(dir);
}

注意,檢查“ isExceptional()”省略了“。”。 和“ ..”條目似乎總是最新的。

完整程序清單:

#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>

#define MAX_LEN 1024

int isExceptionalDir(const char* name){
  if (name==NULL || name[0]=='\0') return 1;
  else if (name[0]=='.') {
    if (name[1]=='\0') return 1;
    else if (name[1]=='.' && name[2]=='\0') return 1;
  }
  return 0;
}

char buffer[MAX_LEN];

void recentByModification(const char* path, char* recent){
  struct dirent* entry;
  time_t recenttime = 0;
  struct stat statbuf;
  DIR* dir  = opendir(path);
  while (NULL != (entry = readdir(dir))) {
    if (!isExceptionalDir(entry->d_name)) {
      sprintf(buffer, "%s/%s", path, entry->d_name);
      stat(buffer, &statbuf);
      if (statbuf.st_mtime > recenttime) {
        strncpy(recent, entry->d_name, MAX_LEN);
        recenttime = statbuf.st_mtime;
      }
    }
  }
  closedir(dir);
}

void recentByName(const char* path, char* recent){
  DIR* dir  = opendir(path);
  struct dirent* entry;
  recent[0] = '\0';
  while (NULL != (entry = readdir(dir))) {
    if (!isExceptionalDir(entry->d_name)) {
      if (strncmp(recent, entry->d_name, MAX_LEN)<0) {
        strncpy(recent, entry->d_name, MAX_LEN);
      }
    }
  }
  closedir(dir);
}


char recent[MAX_LEN];

int main(int argc, const char* args[])
{
  if (argc < 2) {
    printf("Usage: %s path\n", args[0]);
    return 1;
  }
  for (int i=1; i<argc; i++) {
    recentByModification(args[i], recent);
    printf("%s\n", recent);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM