當前位置:首頁 » 編程語言 » c語言獲取文件修改時間
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言獲取文件修改時間

發布時間: 2022-11-20 14:12:36

A. c語言如何獲取文件創建時間

char buf[65];
struct stat;
stat("c:\\test.txt", &sb);
sb.st_ctime 就是文件的創建時間。你可以用localtime()
轉換成立能夠識別的時間。
struct tm* t=localtime(sb.st_ctime);
sprintf(buf, "%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);

B. c語言如何獲得文件的創建時間以及文件的大小(頭文件及所用的函數,最好有用法舉例)

1.MFC中的方法:(C++)

CFileStatus status;
CFile::GetStatus("D:\\test.txt",status);
long lSizeOfFile;
lSizeOfFile = status.m_size;

lSizeOfFile的值就是D:\\test.txt文件的大小

2.標准C獲得文件大小的5種方法
(注意:"__FILE__"指的是當前文件,你可以改為有效路徑的目標文件,比如"D:\\test.txt")
#include "stdafx.h"
#include "stdio.h"
#include <sys/stat.h>
#include <io.h>
#include <FCNTL.H>

int getfilesize()
{
int iresult;
struct _stat buf;
iresult = _stat(__FILE__,&buf);
if(iresult == 0)
{
return buf.st_size;
}
return NULL;
}

int getfilesize01()
{
int fp;
fp=_open(__FILE__,_O_RDONLY);
if(fp==-1)
return NULL;
return _filelength(fp);
//return NULL;
}

int getfilesize02()
{
int fp;
fp=_open(__FILE__,_O_RDONLY);
if(fp==-1)
return NULL;
return _lseek(fp,0,SEEK_END);
//return NULL;
}

int getfilesize03()
{
int fp;
fp=_open(__FILE__,_O_RDONLY);
if(fp==-1)
return NULL;
return _lseek(fp,0,SEEK_END);
//return NULL;
}

int getfilesize04()
{
FILE *fp;
if((fp=fopen(__FILE__,"r"))==NULL)
return 0;
fseek(fp,0,SEEK_END);
return ftell(fp); //return NULL;
}

int getfilesize05()
{
FILE *fp;
char str[1];
if((fp=fopen(__FILE__,"rb"))==NULL)
return 0;
for(int i = 0;!feof(fp);i++)
{
fread(&str,1,1,fp);

}
return i - 1; //return NULL;
}

int main(int argc, char* argv[])
{

printf("getfilesize()=%d\n",getfilesize());
printf("getfilesize01()=%d\n",getfilesize01());
printf("getfilesize02()=%d\n",getfilesize02());
printf("getfilesize03()=%d\n",getfilesize03());
printf("getfilesize04()=%d\n",getfilesize04());
printf("getfilesize05()=%d\n",getfilesize05());
return 0;
}

C. 用C語言怎麼提取文件中的時間信息

首先你有要用FOPEN打開文件
然後用fscanf,fread,fput等IO文件函數讀取
比如C.TXT存在一個時間字元串

int main()
{
char c[];//存放時間的字元數組
FILE* B=fopen("C.TXT"."rt");
fscanf(B,"%s",&c);
printf("%s",c);
getchar();
return 0;
}

D. linux下C語言怎麼獲取文件創建時間

在 Windows 下,一個文件有創建時間、修改時間、訪問時間。而在 Linux 下,一個文件也有三種時間,分別是訪問時間(Access)、修改時間(Modify)、狀態改變時間(Change)。

可以使用 stat 命令查看文件的訪問時間、修改時間和狀態改變時間。

本人使用的機器的磁碟分區使用的文件系統類型是 ext3,也就是說本人是無法查看文件創建時間的。但是,如果文件創建後就沒有修改過,修改時間=創建時間;如果文件創建後,狀態就沒有改變過,那麼狀態改變時間=創建時間;如果文件創建後,沒有被讀取過,那麼訪問時間=創建時間,當這個基本不太可能。

那什麼時候訪問時間,修改時間和狀態改變時間會變化呢?比如我們使用vi打開文件但不編輯,那麼退出後文件的訪問時間就會改變;比如我們使用vi打開文件並且編輯後保存退出,那麼文件的修改時間就會改變,當然訪問時間也改變了;再比如使用chmod +x給文件增加可執行的屬性,那麼文件的狀態改變時間就會改變。

【答題不易,請採納謝謝】

E. c語言如何判斷一個文件是否被修改

可以用 文件狀態 ,例如 文件建立時間,文件最後一次修改時間,文件最後一次被訪問的時間,做判斷。
獲取文件狀態用:
#include <io.h>
int get_namein_time(char *namein, char * ftime){
struct _finddata_t fileinfo;
int res,DEBUG=0,flag=0;
if ( (res = _findfirst(namein, &fileinfo)) == -1){
if (DEBUG==1) printf("get file info error !\n");
return 0;
};
if ( strcmp(namein,fileinfo.name)==0 ) {
flag=1; goto Lab;
}
do {
if ( strcmp(namein,fileinfo.name)==0 ) {flag=1;goto Lab;}
} while ( _findnext(res, &fileinfo) ==0);
Lab: strcpy(ftime,ctime(&fileinfo.time_write));
_findclose(res);
return flag;
}

最可靠的是用文件的哈希碼判斷,就是區塊鏈中用的方法。
例如視窗系統,調用系統 Certutil 計算出 文件的 哈希碼,與文件原來的碼對比。若變了,就是被修改了。
Certutil -hashfile abc.txt MD5 這個檢查 文件 abc.txt
Certutil -hashfile XYZ.txt SHA512 這個檢查 文件 XYZ.txt

F. C語言,在WINDOWS下取文件創建時間的問題

使用_findfirst函數或_findnext函數檢索磁碟上的文件能獲取相關信息,其中就包含創建日期。
long _findfirst(const char*, _finddata_t *);
long _findnext(const long, _finddata_t *);
findfirst函數用一個文件名來啟動一次檢索,同時把能找到的第一個文件的數據存入_finddata_t所指向的結構體,然後返回本次檢索的句柄,若失敗返回-1L
findnext函數則接受一個檢索句柄,尋找下一個有效的相關文件,把數據存入_finddata_t所指向的結構體,然後返回0,若失敗,則返回非零。

G. C語言中time.h頭文件中對時間的操作具體是怎樣的

time.h頭文件提供對時間操作的一些函數,clock()是程序開始到調用的毫秒數。

time_tt_begin,t_end;

t_begin=clock();//記錄開始時間

dosomething();//調用函數

t_end=clock();//記錄結束時間

printf("Timeused=%.21f ",(double)(t_end-t_begin)/CLOCKS_PER_SEC);//顯示函數調用時間

(7)c語言獲取文件修改時間擴展閱讀

c語言中time.h頭文件的使用

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

intmain(void)

{

longi=10000000L;

clock_tstart,finish;

doubleration;//測量一個事件持續的時間

printf("Timetodo%ldemptyloopsis",i);

start=clock();

while(i--)

finish=clock();

ration=(double)(finish-start)/CLOCKS_PER_SEC;//clock()是以毫秒為單位計算時間的所以除以CLOCKS_PER_SEC這是time.h裡面定義的一個常量

printf("%fseconds ",ration);

system("pause");

}

H. C語言:如何得到指定地址的文件夾中所有文件的文件名和其修改時間 包括子文件內的

//獲取指定目錄下的所有文件列表 author:wangchangshaui jlu
char** getFileNameArray(const char *path, int* fileCount)
{
int count = 0;
char **fileNameList = NULL;
struct dirent* ent = NULL;
DIR *pDir;
char dir[512];
struct stat statbuf;

//打開目錄
if ((pDir = opendir(path)) == NULL)
{
myLog("Cannot open directory:%s\n", path);
return NULL;
}
//讀取目錄
while ((ent = readdir(pDir)) != NULL)
{ //統計當前文件夾下有多少文件(不包括文件夾)
//得到讀取文件的絕對路徑名
snprintf(dir, 512, "%s/%s", path, ent->d_name);
//得到文件信息
lstat(dir, &statbuf);
//判斷是目錄還是文件
if (!S_ISDIR(statbuf.st_mode))
{
count++;
}
} //while
//關閉目錄
closedir(pDir);
// myLog("共%d個文件\n", count);

//開辟字元指針數組,用於下一步的開辟容納文件名字元串的空間
if ((fileNameList = (char**) myMalloc(sizeof(char*) * count)) == NULL)
{
myLog("Malloc heap failed!\n");
return NULL;
}

//打開目錄
if ((pDir = opendir(path)) == NULL)
{
myLog("Cannot open directory:%s\n", path);
return NULL;
}
//讀取目錄
int i;
for (i = 0; (ent = readdir(pDir)) != NULL && i < count;)
{
if (strlen(ent->d_name) <= 0)
{
continue;
}
//得到讀取文件的絕對路徑名
snprintf(dir, 512, "%s/%s", path, ent->d_name);
//得到文件信息
lstat(dir, &statbuf);
//判斷是目錄還是文件
if (!S_ISDIR(statbuf.st_mode))
{
if ((fileNameList[i] = (char*) myMalloc(strlen(ent->d_name) + 1))
== NULL)
{
myLog("Malloc heap failed!\n");
return NULL;
}
memset(fileNameList[i], 0, strlen(ent->d_name) + 1);
strcpy(fileNameList[i], ent->d_name);
myLog("第%d個文件:%s\n", i, ent->d_name);
i++;
}
} //for
//關閉目錄
closedir(pDir);

*fileCount = count;
return fileNameList;
}

I. C語言:如何得到指定地址的文件夾中所有文件的文件名和其修改時間 包括子文件內的

俺前段時間寫了段功能相似的程序,但用的是用C++/STL寫的,訪問目錄使用了win32api(能訪問指定目錄的子目錄)。

獲取文件名與修改時間由FileOfDirectory::detectFiles實現(其實你只需要看這一個函數即可)。

這段程序以STL數組保存單個文件名,查詢過程中沒有回溯,wcsstr函數內部也是KMP,所以事實上這個程序也是按KMP查詢的

安時間排序時使用STL演算法庫,時間復雜度同快速排序。

最後,這段代碼是在VS2010編譯的。

#include<vector>

#include<algorithm>

structFileNameAndTime

{

wchar_tszPath[MAX_PATH];//filedirectory

wchar_tszName[MAX_PATH];//filename

FILETIMElastAcc;//lastaccesstime

FileNameAndTime()

{

memset(&lastAcc,0,sizeof(lastAcc));

memset(szName,0,sizeof(wchar_t)*MAX_PATH);

memset(szPath,0,sizeof(wchar_t)*MAX_PATH);

}

FileNameAndTime(constPWCHARfn,constPWCHARpa,constLPFILETIMEft)

{

if((0==fn)||(0==pa)||(0==ft))

return;

memcpy(&lastAcc,ft,sizeof(lastAcc));

wcscpy(szName,fn);

wcscpy(szPath,pa);

}

FileNameAndTime(constFileNameAndTime&fnd)

{

memcpy(&this->lastAcc,&fnd.lastAcc,sizeof(this->lastAcc));

wcscpy(this->szName,fnd.szName);

wcscpy(this->szPath,fnd.szPath);

}

constFileNameAndTime&operator=(constFileNameAndTime&fnd)

{

if(this!=&fnd){

memcpy(&this->lastAcc,&fnd.lastAcc,sizeof(this->lastAcc));

wcscpy(this->szName,fnd.szName);

wcscpy(this->szPath,fnd.szPath);

}

return*this;

}

voidGetFullPath(wchar_t(&fp)[MAX_PATH])const

{

wcscpy(fp,szPath);

wcscat(fp,szName);

}

friendbooloperator>(constFileNameAndTime&l,constFileNameAndTime&r);//comparethisobjectbyaccesstime

};

booloperator<(constFileNameAndTime&l,constFileNameAndTime&r)//forsort

{

if(l.lastAcc.dwHighDateTime<r.lastAcc.dwHighDateTime)

returntrue;

elseif(l.lastAcc.dwHighDateTime==r.lastAcc.dwHighDateTime)

{

if(l.lastAcc.dwLowDateTime<r.lastAcc.dwLowDateTime)

returntrue;

}

returnfalse;

}

classFileOfDirectory

{

private:

staticconstwchar_tszDot[];

staticconstwchar_tszDotDot[];

staticconstwchar_tcStar;

staticconstwchar_tcSlash;

private:

std::vector<FileNameAndTime>vecFT;

wchar_tszCurrentPath[MAX_PATH];

private:

voidvalidatePath(constwchar_t*pPath)

{

wcscpy(szCurrentPath,pPath);

intlen=wcslen(szCurrentPath);

if((cStar!=szCurrentPath[len-1])

&&(cSlash!=szCurrentPath[len-2]))

{

szCurrentPath[len]=cSlash;

szCurrentPath[len+1]=cStar;

szCurrentPath[len+2]=0;

return;

}

if((cStar!=szCurrentPath[len-1])

&&(cSlash==szCurrentPath[len-2]))

{

szCurrentPath[len]=cStar;

szCurrentPath[len+1]=0;

return;

}

}

voiddetectFiles(constLPWSTRszDir)

{

WIN32_FIND_DATAffd;

HANDLEhFind=::FindFirstFile(szDir,&ffd);

if(INVALID_HANDLE_VALUE==hFind)

return;

do

{

if(ffd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)

{

if((0==wcscmp(ffd.cFileName,szDot))||(0==wcscmp(ffd.cFileName,szDotDot)))

continue;

else

{

wchar_tszTempPath[MAX_PATH];

wcscpy(szTempPath,szDir);

szTempPath[wcslen(szTempPath)-1]=0;

wcscat(szTempPath,ffd.cFileName);

intlen=wcslen(szTempPath);

szTempPath[len]=cSlash;

szTempPath[len+1]=cStar;

szTempPath[len+2]=0;

detectFiles(szTempPath);

}

}

else{

wchar_tpath[MAX_PATH];

wcscpy(path,szDir);

path[wcslen(path)-1]=0;

vecFT.push_back(FileNameAndTime(ffd.cFileName,path,&ffd.ftLastAccessTime));

}

}

while(::FindNextFile(hFind,&ffd)!=0);

}

public:

FileOfDirectory(constLPWSTRszDir)

{

validatePath(szDir);

detectFiles(szCurrentPath);

}

voidSortByAccessTime()

{

sort(vecFT.begin(),vecFT.end());

}

intNumOfFiles()const{returnvecFT.size();}

intFindFilesByKeyWord(wchar_t*pszFn,int*outCome,intoutComeLen,boolbMatchAll=false)

{

wchar_tszTemp[MAX_PATH],szFnLwr[MAX_PATH];

intindex=0;

wcscpy(szFnLwr,pszFn);

_wcslwr(szFnLwr);

for(inti=0;i<vecFT.size();++i)

{

wcscpy(szTemp,vecFT[i].szName);

_wcslwr(szTemp);

if(true==bMatchAll)

{

if(0==wcscmp(szTemp,szFnLwr))

{

if(index>=outComeLen)

returnindex;

outCome[index++]=i;

}

}

else

{

if(0!=wcsstr(szTemp,szFnLwr))

{

if(index>=outComeLen)

returnindex;

outCome[index++]=i;

}

}

}

}

FileNameAndTimeGetItemByID(intindex)

{

if((index>=0)&&(index<vecFT.size()))

returnFileNameAndTime(vecFT[index]);

}

};

constwchar_tFileOfDirectory::szDot[]=L".";

constwchar_tFileOfDirectory::szDotDot[]=L"..";

constwchar_tFileOfDirectory::cStar=L'*';

constwchar_tFileOfDirectory::cSlash=L'\';

void__stdcallentp3()//測試程序

{

FileOfDirectoryfod(L"E:\game");

intids[256]={0};

fod.SortByAccessTime();

intlen=fod.FindFilesByKeyWord(L"main",ids,256);

for(inti=0;i<len;++i){

FileNameAndTimefnt(fod.GetItemByID(ids[i]));

CDbgString::OutputDbgStringW(L" %s%s",fnt.szPath,fnt.szName);

}

}

測試結果如圖所示。

J. windows下用C語言怎麼獲得一個文件的最後修改日期

用api函數:GetFileTime
ulong hfile
long rtn
filetime lpcreate
filetime lpaccess
filetime lpwrite
hfile = FileOpen("odbc.ini")
rtn = GetFileTime(hfile, lpcreate, lpaccess, lpwrite)
Messagebox("File Handle", String(hfile))
Messagebox("Return Code", string(rtn))
FileClose(hfile)