MyUtilityOnWin.h
#pragma once
#include <stdio.h>
#include <time.h>
#include <windows.h>
#define OnlyRead 1 //文件必须存在,若不存在,则返回NULL
#define ReadWrite 2 //清空文件内容,若文件不存在就创建
#define Append 3 //以追加方式打开读写文件,若文件不存在就创建
#define FileHead 1 //文件开头
#define FileCurrent 2 //文件读写指针当前位置
#define FileEnd 3 //文件末尾
#ifdef __cplusplus
extern "C"
{
#endif
FILE* Open(const char* filename, int opt);
int Close(FILE* fp);
size_t Read(FILE* fp, void* readBuf, size_t nBytes);
size_t Write(FILE* fp, void* writeBuf, size_t nBytes);
int Seek(FILE* fp, int nStartPos, int offset);
//start和finish通过调用clock函数直接得到
double GetCodeRunTime(clock_t start, clock_t finish);
//产生[down,up]范围的随机整数
int SpecRangeRand(int down, int up);
void ShowSystemError(HWND hwndMsgOwner, DWORD dwError);
void PrintSystemError(DWORD dwError);
/*
启动线程,此函数返回的线程句柄必须由调用者调用CloseHandle显示关闭,threadfunc原型为:
unsigned __stdcall threadfunc(void* pArguments);
*/
uintptr_t SetupThread(unsigned (__stdcall *threadfunc)(void *), void *arg, unsigned initflag, unsigned *threadId);
#ifdef __cplusplus
}
#endif
MyUtilityOnWin.c
#include "MyUtilityOnWin.h"
//#include <locale.h>
size_t Read(FILE* fp, void* readBuf, size_t nBytes)
{
return fread(readBuf, 1, nBytes, fp);
}
size_t Write(FILE* fp, void* writeBuf, size_t nBytes)
{
return fwrite(writeBuf, 1, nBytes, fp);
}
FILE* Open(const char* filename, int opt)
{
FILE* fp = NULL;
switch (opt)
{
case OnlyRead:
fp = fopen(filename, "rb");
break;
case Append:
fp = fopen(filename, "ab+");
break;
case ReadWrite:
fp = fopen(filename, "wb+");
break;
}
return fp;
}
int Close(FILE* fp)
{
return fclose(fp);
}
//int Remove(FILE* fp)
//{
// return remove(fp);
//}
int Seek(FILE* fp, int nStartPos, int offset)
{
int ret = -1;
switch (nStartPos)
{
case FileCurrent:
ret = fseek(fp, offset, SEEK_CUR);
break;
case FileEnd:
ret = fseek(fp, offset, SEEK_END);
break;
case FileHead:
ret = fseek(fp, offset, SEEK_SET);
break;
}
return ret;
}
void ShowSystemError(HWND hwndMsgOwner, DWORD dwError)
{
HLOCAL hlocal = NULL;
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), (TCHAR*)&hlocal, 0, NULL);
MessageBox(hwndMsgOwner, (TCHAR*)LocalLock(hlocal), 0, 0);
LocalFree(hlocal);
}
void PrintSystemError(DWORD dwError)
{
HLOCAL hlocal = NULL;
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), (char*)&hlocal, 0, NULL);
//setlocale(LC_ALL, "chs");
printf((char*)LocalLock(hlocal));
LocalFree(hlocal);
}
double GetCodeRunTime(clock_t start, clock_t finish)
{
double duration = 0;
duration = (double)(finish - start) / CLOCKS_PER_SEC;
return duration;
}
int SpecRangeRand(int down,int up)
{
int num = 0;
num = rand() % (up - down +1) + down;
return num;
}
uintptr_t SetupThread(unsigned (__stdcall *threadfunc)(void *),void *arg,unsigned initflag,unsigned *threadId)
{
return _beginthreadex(NULL, 0, threadfunc, arg, initflag, threadId);
}