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)