文件搜索+路径设置+文件属性

//【直接上可以运行的代码文件】

//文件搜索+路径设置+文件属性.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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值