MyUtilityOnWin

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值