C++ 注册表取值 按行读取txt文件 时间差天数 格林威治时间转标准时间

导读:本篇文章讲解 C++ 注册表取值 按行读取txt文件 时间差天数 格林威治时间转标准时间,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

// regedit.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"//vs新建项目自动生成
#include <iostream>
#include <assert.h>
#include "windows.h"
#include "tchar.h"
#include "conio.h"
#include "stdio.h"
//file
#include <fstream>
#include <string>
//time
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;


void wcharTochar(const wchar_t *wchar, char *chr, int length)//宽字符转char
{
	WideCharToMultiByte(CP_ACP, 0, wchar, -1,
		chr, length, NULL, NULL);
}

bool OpenRegKey(HKEY& hRetKey)//打开注册表
{
	LPCWSTR sw = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Bandizip.exe");//_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Bandizip.exe");
	wprintf(L"SW is %s\n",sw);
	if (ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE, sw, &hRetKey))
	{
		return true;
	}
	printf("OpenRegKey return is false!\n");
	return false;
}

bool QueryRegKey(LPCWSTR strSubKey, LPCWSTR strValueName, char *strValue,int length)//从注册表里取值
{
	DWORD dwType = REG_SZ;//定义数据类型
	DWORD dwLen = MAX_PATH;

	wchar_t data[MAX_PATH];
	HKEY hKey;
	HKEY hSubKey;
	if (OpenRegKey(hKey))
	{
		if (ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE, strSubKey, &hSubKey))
		{
			TCHAR buf[256] = { 0 };
			
			if (ERROR_SUCCESS == RegQueryValueEx(hSubKey, strValueName, 0, &dwType, (LPBYTE)data, &dwLen))
			{
				wcharTochar(data, strValue,length);
				wprintf(L"data = %s,len= %d\n", data,strlen((const char *)data));
				RegCloseKey(hKey); //关闭注册表
				return true;
			}
		}
		RegCloseKey(hKey); //关闭注册表
	}

	return false;
}
//文件

int CountLines(char *filename)
{
	ifstream ReadFile;
	int n = 0;
	string tmp;
	ReadFile.open(filename, ios::in);//ios::in 表示以只读的方式读取文件
	if (ReadFile.fail())//文件打开失败:返回0
	{
		return 0;
	}
	else//文件存在
	{
		while (getline(ReadFile, tmp, '\n'))
		{
			n++;
		}
		ReadFile.close();
		return n;
	}
}

string ReadLine(char *filename, int line)
{
	int lines, i = 0;
	string temp;
	fstream file;
	file.open(filename, ios::in);
	lines = CountLines(filename);

	if (line <= 0)
	{
		return "Error 1";
	}
	if (file.fail())
	{
		return "Error 2";
	}
	if (line > lines)
	{
		return "Error 3";
	}
	while (getline(file, temp) && i < line - 1)
	{
		i++;
	}
	file.close();
	return temp;
}
//time
/** @fn: int CHttpUtil::GetLocalTime(char* szLocTime)
 *  @brief : 获取系统本地时间
 *  @param (out) char * szLocTime: 存放本地时间的缓存区,外部传入
 *  @return int : szLocTime的实际长度
 */
int GetLocalTime(char* szLocTime)//获取本地标准时间
{
	if (szLocTime == NULL)
	{
		return -1;
	}
	time_t rawTime;
	struct tm* timeInfo;
	char szTemp[30] = { 0 };

	time(&rawTime);
	timeInfo = localtime(&rawTime);
	strftime(szTemp, sizeof(szTemp), "%Y-%m-%d-%H-%M-%S", timeInfo);

	strcpy_s(szLocTime, strlen(szTemp) + 1, szTemp);
	return strlen(szLocTime);
}
int stamp_to_standard(time_t nSrc, char *sDestTime)//格林威治时间转标准时间
{
	struct tm p;
	p = *localtime(&nSrc);
	strftime(sDestTime, 1000, "%Y-%m-%d-%H-%M-%S", &p);
	return 0;
}
time_t convert(int year, int month, int day,int H,int M,int S)
{
	tm info = { 0 };
	info.tm_year = year-1900;
	info.tm_mon = month-1;
	info.tm_mday = day;
	info.tm_sec=S;   // seconds after the minute - [0, 60] including leap second
	info.tm_min=M;   // minutes after the hour - [0, 59]
	info.tm_hour= H;
	return mktime(&info);
}
int get_days(const char* from, const char* to)
{
	int year, month, day,H,M,S;
	sscanf(from, "%d-%d-%d-%d-%d-%d", &year, &month, &day, &H, &M, &S);
	printf("163--------------%d-%d-%d-%d-%d-%d\n", year, month, day, H, M, S);
	int fromSecond = (int)convert(year, month, day, H, M, S);
	printf("--------------%d\n", fromSecond);
	sscanf(to, "%d-%d-%d-%d-%d-%d", &year, &month, &day, &H, &M, &S);
	int toSecond = (int)convert(year, month, day, H, M, S);
	printf("%d\n", toSecond);
	return (toSecond - fromSecond) / 24 / 3600;
}
int main()
{
	HKEY  hKey = NULL;
	string result;
	LPCWSTR strSubKey= _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Bandizip.exe");
	LPCWSTR strValueName= _T("Path");
	char strValue[256];
	int length = 256;
	bool status = QueryRegKey(strSubKey,strValueName,strValue,length);
	printf("status is %d\n", status);
	printf("result is %s\n", strValue);
	
	int line;
	char filename[] = "C:\\Program Files\\Bandizip\\savapi\\update_AVIRA.txt";
	string result_file;
	
	line = CountLines(filename);
	if (line > 0)
	{
		result_file = ReadLine(filename, 1);
		cout << result_file << endl;
	}
	else
	{
		cout<<"no file"<<endl;
	}
	//旧
	time_t a = 1343976859;
	stamp_to_standard(a, strValue);
	printf("time is %s\n", strValue);
//本地时间
	char szTemp[30] = { 0 };
	GetLocalTime(szTemp);
	printf("szTemp is %s\n", szTemp);

	int days = get_days(strValue, szTemp);
	printf("From:%s\nTo:%s\n", strValue, szTemp);
	printf("%d\n", days);
	return 0;
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/96600.html

(0)
小半的头像小半

相关推荐

极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!