一个实例 c++ VS2017 读取当前系统时间 以时间来命名txt文件

实例代码

#include <ctime>
#include <iostream>
#include <string>
#pragma warning(disable:4996)

using namespace std;

/*
编译时出现以下报错,
C4996 'localtime': This function or variable may be unsafe. Consider using localtime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS
解决办法:
在头文件处,增加:#pragma warning(disable:4996)

相关的解决办法,详见以下链接
https://blog.csdn.net/weixin_40937100/article/details/88809384
*/

int main()
{
	string str_year, str_mon, str_mday, str_hour, str_min, str_sec;
	string str_now;
	string str_filename;

	time_t now = time(0);
	tm *this_monent = localtime(&now);

	//tm结构的变量都属于int
	cout << "这一刻属于哪一年=" << 1900 + this_monent->tm_year << "\n";
	cout << "这一刻属于该年哪个月=" << this_monent->tm_mon << "\n";
	cout << "这一刻属于该月哪一日=" << this_monent->tm_mday << "
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的例子,演示了如何在两个进程之间使用命名管道传递消息。 首先,我们需要创建一个命名管道,代码如下: ```c++ HANDLE hPipe; LPTSTR lpPipeName = TEXT("\\\\.\\pipe\\MyPipe"); hPipe = CreateNamedPipe(lpPipeName, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 1024, 1024, NMPWAIT_USE_DEFAULT_WAIT, NULL); if (hPipe == INVALID_HANDLE_VALUE) { // 处理错误 } ``` 在这个例子中,我们使用 `CreateNamedPipe` 函数创建了一个名为 `MyPipe` 的命名管道。管道的访问模式为双向访问,类型为消息型管道,读取模式为消息读取模式,等待模式为默认等待模式。管道的实例数量为无限制,输入缓冲区大小和输出缓冲区大小均为1024字节。 接下来,我们在一个进程中向管道中写入消息: ```c++ DWORD dwWritten; char szMessage[] = "Hello, World!"; if (!ConnectNamedPipe(hPipe, NULL)) { // 处理错误 } if (!WriteFile(hPipe, szMessage, strlen(szMessage), &dwWritten, NULL)) { // 处理错误 } FlushFileBuffers(hPipe); DisconnectNamedPipe(hPipe); ``` 在这个例子中,我们使用 `ConnectNamedPipe` 函数等待客户端连接管道。一旦客户端连接上管道,我们通过 `WriteFile` 函数将消息写入管道。最后,我们使用 `FlushFileBuffers` 函数和 `DisconnectNamedPipe` 函数断开管道连接。 在另一个进程中,我们可以从管道中读取消息,代码如下: ```c++ HANDLE hPipe; LPTSTR lpPipeName = TEXT("\\\\.\\pipe\\MyPipe"); hPipe = CreateFile(lpPipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hPipe == INVALID_HANDLE_VALUE) { // 处理错误 } char szMessage[1024]; DWORD dwRead; if (!ReadFile(hPipe, szMessage, sizeof(szMessage), &dwRead, NULL)) { // 处理错误 } printf("Received message: %s\n", szMessage); CloseHandle(hPipe); ``` 在这个例子中,我们使用 `CreateFile` 函数连接到名为 `MyPipe` 的命名管道。一旦连接成功,我们可以使用 `ReadFile` 函数从管道中读取消息。最后,我们通过 `printf` 函数打印出收到的消息,并关闭管道连接。 这就是一个简单的例子,演示了如何在两个进程之间使用命名管道传递消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值