從系統文件系統獲取文件信息

#include<iostream>
#include<string>
#include<ctime>
#include<vector>
#include<Windows.h>
#include<io.h>
#include<WinBase.h>
#include<stdio.h>

using namespace std;


string DeTime_t(const time_t t)
{
	string sp;
	if (t == 0)
	{
		sp = "1970/1/1 0:0:0";
	}
	else if (t != 0)
	{
		tm* m_t = localtime(&t);
		sp = to_string(m_t->tm_year + 1900) + "/" + to_string(m_t->tm_mon) + "/" + to_string(m_t->tm_mday) + " " +
			to_string(m_t->tm_hour) + ":" + to_string(m_t->tm_min) + ":" + to_string(m_t->tm_sec);
	}

	return sp;
}


void GetFileInfoFormFS(const char* filename)
{
	struct _finddata_t   fileInfo;
	memset(&fileInfo, 0, sizeof(fileInfo));
	intptr_t fhandle = _findfirst(filename, &fileInfo);
	if (-1L == fhandle)
	{
		cout << "找不到文件\n";
	}
	_findclose(fhandle);
	cout << "文件名:" << fileInfo.name << endl;
	cout << "文件大小:" << fileInfo.size << endl;
	cout << "創建時間:" << DeTime_t(fileInfo.time_create) << endl;
	cout << "修改時間:" << DeTime_t(fileInfo.time_write) << endl;
	cout << "最近訪問時間:" << DeTime_t(fileInfo.time_access) << endl;
	cout << "文件屬性:" << fileInfo.attrib << endl;
}

size_t FileSize(const char* filename)
{
	struct _finddata_t fileInfo = { 0 };
	intptr_t fhandle = _findfirst(filename, &fileInfo);
	if (-1L == fhandle)
	{
		cout << "找不到文件!\n";
	}
	return fileInfo.size;
}

void getFiles(string path,vector<string> files)
{
	intptr_t hfile = 0;//文件句柄
	struct _finddata_t fileinfo;
	string tmp;
	hfile = _findfirst(path.c_str(), &fileinfo);
	if (-1 != hfile)
	{
		do
		{
			if (fileinfo.attrib & _A_SUBDIR)//如果是目錄,迭代之             
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
					getFiles(tmp.assign(path).append("\\").append(fileinfo.name), files);
			}
			else //如果不是,加入列表
			{
				files.push_back(tmp.assign(path).append("\\").append(fileinfo.name));
			}

		} while (_findnext(hfile, &fileinfo) == 0);
		_findclose(hfile);
	}
}
void gettime()
{
	time_t rawtime;
	struct tm* ptminfo;
	time(&rawtime);
	ptminfo = localtime(&rawtime);
	printf("current: %02d-%02d-%02d %02d:%02d:%02d\n",
		ptminfo->tm_year + 1900, ptminfo->tm_mon + 1, ptminfo->tm_mday,
		ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec);
}

void GetComputerTime()
{
	LPSYSTEMTIME p_st = (LPSYSTEMTIME)malloc(sizeof(SYSTEMTIME));
	GetLocalTime(p_st);
	cout << "現在時間:" << p_st->wYear << "/" << p_st->wMonth << "/" << p_st->wDay << " "
		<< p_st->wHour << ":" << p_st->wMinute << ":" << p_st->wSecond << endl;
	//printf("%d:%d:%d\n", p_st->wHour, p_st->wMinute, p_st->wSecond);
	free(p_st);
}
int main()
{
	gettime();
	GetComputerTime();
	GetFileInfoFormFS("res/1.jpg");
	GetFileInfoFormFS("res/2.jpg");
	GetFileInfoFormFS("res/3.jpg");
	GetFileInfoFormFS("res/4.jpg");
	GetFileInfoFormFS("res/5.jpg");
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章