日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

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

發(fā)布時間:2025/3/19 c/c++ 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。