C++ 注册表取值 按行读取txt文件 时间差天数 格林威治时间转标准时间
生活随笔
收集整理的這篇文章主要介紹了
C++ 注册表取值 按行读取txt文件 时间差天数 格林威治时间转标准时间
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
// regedit.cpp : 此文件包含 "main" 函數(shù)。程序執(zhí)行將在此處開始并結(jié)束。
//#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)//寬字符轉(zhuǎn)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;//定義數(shù)據(jù)類型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 : 獲取系統(tǒng)本地時間* @param (out) char * szLocTime: 存放本地時間的緩存區(qū),外部傳入* @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)//格林威治時間轉(zhuǎn)標準時間
{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 secondinfo.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;
}
?
總結(jié)
以上是生活随笔為你收集整理的C++ 注册表取值 按行读取txt文件 时间差天数 格林威治时间转标准时间的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QT udp自动获取对方ip和端口号
- 下一篇: c++ 截取\r\n问题