//【直接上可以运行的代码文件】
//文件搜索+路径设置+文件属性.cpp
#include<iostream.h>
#include<time.h>
#include<stdio.h>
#include<direct.h>
#include<dos.h>
#include<io.h>
#include<string.h>
#include<stdlib.h>
#include <windows.h>
#define CWDlen 100//当前目录长度
char* longTime2charTime(time_t longTime)
{
tm * tmTime;
tmTime=localtime(&longTime);
//注意下面两句话的区别
//char charTime[CWDlen]; //
char *cTime=(char *)malloc(sizeof(char)*CWDlen); //用malloc函数申请空间,函数返回之后空间就不会被释放掉了
sprintf(cTime,"%d.%d.%d.%d.%d.%d",tmTime->tm_year+1900,tmTime->tm_mon+1,tmTime->tm_mday,tmTime->tm_hour,tmTime->tm_min,tmTime->tm_sec);
//printf("时间=%s\n",cTime);
return cTime;
}
int main()
{
time_t now;
time(&now);
cout<<"1当前时间="<<now<<endl;
char* charTime=longTime2charTime(now);
cout<<"2当前时间="<<charTime<<endl;
char* strnow;
strnow=ctime(&now);
cout<<"3当前时间="<<strnow;//已经含有换行符了
char bufPN[CWDlen];
GetCurrentDirectory(1000,bufPN); //当前目录<windows.h>
cout<<"1当前路径="<<bufPN<<endl;
SetCurrentDirectory(strcat(bufPN,"\\Debug\\")); //设置当前目录为当时目录下的Debug目录
GetCurrentDirectory(1000,bufPN); //得到当前工作路径
cout<<"2当前路径="<<bufPN<<endl;
SetCurrentDirectory(strcat(bufPN,"\\..\\Debug\\")); //设置当前目录为当时的上一级下的Debug目录
GetCurrentDirectory(1000,bufPN); //得到当前工作路径
cout<<"3当前路径="<<bufPN<<endl;
//搜索文件
_finddata_t ffblk;
_findfirst("*.exe",&ffblk);//在当前目录搜索第一个*.exe文件
//显示找到的文件的信息
GetCurrentDirectory(1000,bufPN); //当前目录<windows.h>
cout<<"搜索目录="<<bufPN<<endl;
cout<<"文件名="<<ffblk.name<<endl;
cout<<"文件大小="<<ffblk.size<<endl;
cout<<"文件属性="<<ffblk.attrib<<endl;
cout<<"文件创建时间"<<longTime2charTime(ffblk.time_create)<<endl;
cout<<"文件访问时间"<<longTime2charTime(ffblk.time_access)<<endl;
cout<<"文件修改时间"<<longTime2charTime(ffblk.time_write)<<endl;
char bufCWD[CWDlen];
char* cwd;
cwd=getcwd(bufCWD,CWDlen);//当前目录<direct.h>
if(cwd)
{
cout<<"4当前路径="<<bufCWD<<endl;
}
else
{
cout<<"当前路径获取失败!"<<endl;
}
char bufFN[CWDlen];
sprintf(bufFN,"\\%s",ffblk.name);
char* fn=strcat(cwd,bufFN);
cout<<"文件全名="<<fn<<endl;
return 0;
}