尝试获取文件属性,但及时收到错误 [英] Try to Get File Attribute, but getting error in time

查看:64
本文介绍了尝试获取文件属性,但及时收到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者,尝试在文件夹中获取文件属性,创建路径并将数据推回到结构中。但是我没有正确地修改文件小时(Hr),它显示7的差异,min amd sec geeting correct ,请有人帮助我。在我的代码下面。在st变量中的值。





Hi am beginner,trying to get file attribute inside the folder, create a path and data push-back into structure.But am not getting File modified hour(Hr) correctly, it showing difference of 7,min amd sec geeting correct,Please anyone be helpful to me. and below my code.Values in "st" variable.


char exePath[300];

struct FileProperty
{
	string fileName,fileModi;
	float fileSize;
};

vector <fileproperty> filePropertyObj;

void readFileAttributes(string filePath,string extention,bool returnStructure)
{
	long hFile;
	struct _finddata_t Rt_file1;
	char path[300],path1[300];
	vector<string>fileNam;
	
	sprintf(path,"%s",filePath.c_str());	
	SetCurrentDirectoryA(path);
	GetCurrentDirectoryA(260,path1);
	char ext[10];
	sprintf(ext,"%s","*.*");
	if(strcmp(extention.c_str(),"NULL"))	{
	     sprintf(ext,"%s",extention.c_str());
	}
	if((hFile = _findfirst(ext, &Rt_file1)) == -1L)   {

	} else	{
		fileNam.push_back(Rt_file1.name);
		while(_findnext(hFile, &Rt_file1) == 0)	{
		fileNam.push_back(Rt_file1.name);
		}
	}
	
	BY_HANDLE_FILE_INFORMATION fileinfo;
	for(int i=0; i<filenam.size();>		char file[300];
		sprintf(file,"%s",fileNam[i].c_str());
		// clear everything in the structure, this is optional
		ZeroMemory(&fileinfo, sizeof(BY_HANDLE_FILE_INFORMATION));
		// obtain a handle to the file, in this case the file
		// must be in the same directory as your application
		HANDLE myfile = NULL;
		myfile = CreateFileA(file,0x00,0x00,NULL,
							 OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
		if(myfile!=INVALID_HANDLE_VALUE)	{
			//try to fill the structure with info regarding the file
			if(GetFileInformationByHandle(myfile, &fileinfo))	{
				SYSTEMTIME st;
				FileTimeToSystemTime(&fileinfo.ftLastWriteTime,&st);
				FileProperty filePpyObj;
				filePpyObj.fileName = fileNam[i];				//File Name
				char dateTime[50] = {0};
				sprintf(dateTime,"%d/%d/%d %d:%d:%d",st.wDay,st.wMonth,st.wYear,st.wHour,st.wMinute,st.wSecond);
				
				filePpyObj.fileModi = dateTime;					// File modified Date
				
				filePpyObj.fileSize = fileinfo.nFileSizeLow;	// File size

				filePropertyObj.push_back(filePpyObj);

			}
		 }
        // you should close the handle once finished
        CloseHandle(myfile);
	}
	SetCurrentDirectoryA(exePath);
	//GetFileAttributesExW
}

int main()
{
	char filePath[300];
	sprintf(filePath,"%s","D:\Coastal");
	GetCurrentDirectoryA(260,exePath);
	readFileAttributes(filePath,"*.DAT",true);
	return 0;
}

推荐答案

查看 FileTimeToSystemTime [ ^ ]:

See the first sentence at FileTimeToSystemTime[^]:
Quote:

将文件时间转换为系统时间格式。系统时间基于协调世界时(UTC)。

Converts a file time to system time format. System time is based on Coordinated Universal Time (UTC).

时间以UTC为单位,而不是根据本地时区。您可以使用 SystemTimeToTzSpecificLocalTime [ ^ ]将其转换为当地时间。

The time is in UTC and not according to the local time zone. You may use SystemTimeToTzSpecificLocalTime[^] to convert it to local time.


这篇关于尝试获取文件属性,但及时收到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆