VS C++ 常用函数

获取当前时间

#include <Winbase.h> 或者 #include<Windows.h>

void WINAPI GetLocalTime(
  __out LPSYSTEMTIME lpSystemTime
);

typedef struct _SYSTEMTIME {  
	WORD wYear;  		/* 有效值 1601 到 30827 */
	WORD wMonth;  		/* 1(January),2(February),3(March),4(April),5(May),6(June),7(July),8(August),9(September),10(October),11(November),12(December) */
	WORD wDayOfWeek; 	/* 0(Sunday), 1(Monday), 2(Tuesday), 3(Wednesday), 4(Thursday), 5(Friday), 6(Saturday)*/ 
	WORD wDay;  		/* 有效值 1 到 31 */
	WORD wHour;  		/* 有效值 0 到 23 */
	WORD wMinute;  		/* 有效值 0 到 59 */
	WORD wSecond;  		/* 有效值 0 到 59 */
	WORD wMilliseconds;	/* 有效值 0 到 999 */
} SYSTEMTIME,  *PSYSTEMTIME;

例程

#include <Windows.h>
#include <stdio.h>

int main(int argc, char **argv)
{
	SYSTEMTIME sysTime;
	GetLocalTime(&sysTime);
	printf("%d/%d/%d/%d - %d:%d:%d:%d\n", sysTime.wYear, sysTime.wMonth, sysTime.wDay
			,sysTime.wDayOfWeek, sysTime.wHour, sysTime.wMinute, sysTime.wSecond
			,sysTime.wMilliseconds);
	getchar();
	return 0;
}

原子操作,变量增1

#include<Winbase.h> 或 #include <Windows.h>

LONG __cdecl InterlockedIncrement(
  __in_out LONG volatile* Addend
);

例程

#include <Windows.h>
#include <stdio.h>

int main(int argc, char **argv)
{

	long inc = 0;
	InterlockedIncrement(&inc);
	printf("inc = %d\n", inc);

	getchar();
	return 0;
}

互斥锁

#include <Winbase.h> 或者 #include <Windows.h>
HANDLE WINAPI CreateMutex(
  __in LPSECURITY_ATTRIBUTES lpMutexAttributes,	// 属性
  __in BOOL bInitialOwner,	// true 互斥体的创建者为拥有者, false 互斥体的创建者不拥有
  __in LPCTSTR lpName	// 互斥锁的名称
);
返回值:
	成功,返回互斥锁的句柄
	失败,返回NULL
	互斥锁的 lpName 名称存在  GetLastError 返回 ERROR_ALREADY_EXISTS 

例程

#include <Windows.h>
#include <stdio.h>

int main(int argc, char **argv)
{
	HANDLE hMutex = NULL;
	hMutex = CreateMutex(NULL, FALSE, (LPCTSTR)"TestMutex");
	if ( GetLastError() == ERROR_ALREADY_EXISTS )
	{
		printf("TestMutex already exist\n");
		CloseHandle(hMutex);
		hMutex = NULL;
	}
	 

	CloseHandle(hMutex);
	getchar();
	return 0;
}

查看当前运行进程路径(不包括文件名)

DWORD WINAPI GetCurrentDirectory(
  __in DWORD nBufferLength,		// lpBuffer 的大小
  __out LPTSTR lpBuffer		// 返回路径内容
);
返回值
	成功,返回写入lpBuffer的字节数
	失败,返回0
查看当前运行进程路径(包括文件名)
DWORD WINAPI GetModuleFileName(
  __in HMODULE hModule,		// NULL, 获取当前进程的路径
  __out LPTSTR lpFilename,	// 返回路径名
  __in DWORD nSize		// lpFilename 的大小
);
返回值
	成功,返回写入 lpFilename 的字节数
	失败,返回0

例程

#include <windows.h> 
#include <stdio.h>
 
int main(int argc, char* argv[])
{
    char szCurPath[128] ;
   GetCurrentDirectory(sizeof(szCurPath),  szCurPath);
   printf("%s\n", szCurPath);
   
   GetModuleFileName(NULL,szCurPath, sizeof(szCurPath));
   printf("%s\n", szCurPath);

  getchar();
  return 0;
}
注意:如果打印不出来,修改

追加字符串

char *strcat( char *strDestination, const char *strSource );
返回值
	返回拼接后的字符串,错误无返回值
例程
#include <Windows.h>
#include <stdio.h>

int main(int argc, char **argv)
{
	char szBuf[128] = "hello";
	//strcat(szBuf, " world!");
	printf("%s\n", strcat(szBuf, " world!"));
	printf("%s\n", szBuf);

	getchar();
	return 0;
}

查看路径是否为目录

DWORD GetFileAttributes(  LPCTSTR lpFileName 	// 路径名
); 	
返回值:
	FILE_ATTRIBUTE_ARCHIVE
	FILE_ATTRIBUTE_COMPRESSED
	FILE_ATTRIBUTE_DIRECTORY		目录
	FILE_ATTRIBUTE_ENCRYPTED
	FILE_ATTRIBUTE_HIDDEN
	FILE_ATTRIBUTE_INROM
	FILE_ATTRIBUTE_NORMAL
	FILE_ATTRIBUTE_READONLY
	FILE_ATTRIBUTE_ROMMODULE
	FILE_ATTRIBUTE_ROMSTATICREF
	FILE_ATTRIBUTE_SYSTEM
	FILE_ATTRIBUTE_TEMPORARY

例程

#include <Windows.h>
#include <stdio.h>


int main(int argc, char **argv)
{
	if ( GetFileAttributes("./") == FILE_ATTRIBUTE_DIRECTORY) {
		printf("current path is directory\n");
	} else {
		printf("current path is not directory\n");
	}
	

	getchar();
	return 0;
}

ifstream 用法

#include <fstream>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
    ifstream ifs("./basic_ifstream_class.txt");
    if (!ifs.bad())
    {
        cout << ifs.rdbuf();	// 输出读取的文件内容
        ifs.close();
    }

	getchar();
	return 0;
}
// ./basic_ifstream_class.txt
hello


#include <fstream>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
    std::ifstream ifs("./basic_ifstream_class.txt", std::ifstream::in);
    if (ifs.good())
    {
        cout << ifs.rdbuf();	// 输出读取的文件内容
        ifs.close();
    }

	getchar();
	return 0;
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值