将当前时间输出到txt中:

调用c++中的fstream流文件,用tm结构获取日期和时间,其在time.h中定义

用ofstream的时候,ofstream out(txtpath,ios::app);    //追加的方式写入    ofstream out(txtpath)  //打开文件,从零开始写入

#include <iostream>
#include <time.h>
#include <fstream>
using namespace std; void writeTimeToTxt(string txtname)
{
ofstream fid(txtname); time_t tt = time(NULL);
struct tm local_time;
localtime_s(&local_time, &tt); fid
<< "***testing date: "
<< local_time.tm_year + << "-" << local_time.tm_mon + << "-" << local_time.tm_mday << " "
<< local_time.tm_hour << ":" << local_time.tm_min << ":" << local_time.tm_sec << endl;
fid.close(); return;
} int main()
{
string txtname = "time.txt";
writeTimeToTxt(txtname); return ;
}

输出的结果:

将当前时间以字符串的形式返回:

#include <time.h>

string get_time_string()
{
time_t tt = time(NULL);
struct tm local_time;
localtime_s(&local_time, &tt);
clock_t now_clock = clock();
int msecond = now_clock % CLOCKS_PER_SEC; char text[];
sprintf(text, "%d%d%d%d%d%d%03d", local_time.tm_year + , local_time.tm_mon + , local_time.tm_mday,
local_time.tm_hour, local_time.tm_min, local_time.tm_sec, msecond); return text;
}

通过argv向程序中输入参数:

#include <iostream>
#include <string>
using namespace std; int main(int argc, char* argv[]) { if ( != argc)
{
cout << "input params error" << endl;
return -;
} string type = argv[];
string cutmethod = argv[];
string resolution = argv[]; cout << type << "_" << cutmethod << "_" << resolution << endl; system("pause");
return ;
}

输出结果:

在vs中参数的输入,项目-属性-调试-命令参数-train expand 64x64          输入的字符串,双引号可加可不加

生成的exe调用过程中,.\OutputTimeToTxt.exe "train" expand 64x64     字符串的双引号,可加可不加

linux:

以微秒为单位,返回string类型的时间戳,并以时间戳为名称,建立文件夹

#include <iostream>
#include <sys/time.h>
#include <unistd.h>
#include <string> #include <sys/stat.h>
#include <sys/types.h> std::string get_nowtime_linux()
{
struct timeval tv;
gettimeofday(&tv,NULL);
std::string now_time=std::to_string(tv.tv_sec)+"_"+std::to_string(tv.tv_usec);
return now_time;
} int main(int argc,char** argv)
{
std::string now_time=get_nowtime_linux();
std::cout<<now_time<<std::endl; int isCreate = mkdir(now_time.c_str(),S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);
if( !isCreate )
printf("create path:%s\n",now_time.c_str());
else
printf("create path failed! error code : %d %s \n",isCreate,now_time.c_str()); return ;
}

 二维数组传引用:

#include <iostream>
using namespace std; /*传二维数组*/ //第1种方式:传数组,第二维必须标明
/*void display(int arr[][4])*/
void display1(int arr[][4], const int irows)
{
for (int i = 0; i<irows; ++i)
{
for (int j = 0; j<4; ++j)
{
cout << arr[i][j] << " "; //可以采用parr[i][j]
arr[i][j] += 1;
}
cout << endl;
}
cout << endl;
} //第2种方式:一重指针,传数组指针,第二维必须标明
/*void display(int (*parr)[4])*/
void display2(int(*parr)[4], const int irows)
{
for (int i = 0; i<irows; ++i)
{
for (int j = 0; j<4; ++j)
{
cout << parr[i][j] << " "; //可以采用parr[i][j]
parr[i][j] += 1;
}
cout << endl;
}
cout << endl;
}
//注意:parr[i]等价于*(parr+i),一维数组和二维数组都适用 //第3种方式:传指针,不管是几维数组都把他看成是指针
/*void display3(int *arr)*/
void display3(int *arr, const int irows, const int icols)
{
for (int i = 0; i<irows; ++i)
{
for (int j = 0; j<icols; ++j)
{
cout << *(arr + i*icols + j) << " "; //注意:(arr+i*icols+j),不是(arr+i*irows+j)
*(arr + i*icols + j) += 1;
}
cout << endl;
}
cout << endl;
} int main()
{
int arr[][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
int irows = 3;
int icols = 4;
//display1(arr, irows);
display2(arr, irows); //注意(int*)强制转换.个人理解:相当于将a拉成了一维数组处理。
//display3((int*)arr, irows, icols);
for (int i = 0; i < irows; i++)
{
for (int j = 0; j < icols; j++)
printf("%d ",arr[i][j]);
printf("\n");
} system("pause");
return 0;
}

  

  

C++实现的一些功能代码的更多相关文章

  1. 原生JS实现购物车结算功能代码+zepto版

    html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  2. 常见.NET功能代码汇总 (2)

    常见.NET功能代码汇总 23,获取和设置分级缓存 获取缓存:首先从本地缓存获取,如果没有,再去读取分布式缓存写缓存:同时写本地缓存和分布式缓存 private static T GetGradeCa ...

  3. 通过javascript库JQuery实现页面跳转功能代码

    通过javascript库JQuery实现页面跳转功能代码的四段代码实例如下. 实例1: 1 2 3 4 $(function(){ var pn = $("#gotopagenum&quo ...

  4. IOS开发-OC学习-常用功能代码片段整理

    IOS开发-OC学习-常用功能代码片段整理 IOS开发中会频繁用到一些代码段,用来实现一些固定的功能.比如在文本框中输入完后要让键盘收回,这个需要用一个简单的让文本框失去第一响应者的身份来完成.或者是 ...

  5. 20个开发人员非常有用的Java功能代码

    本文将为大家介绍20个对开发人员非常有用的Java功能代码.这20段代码,可以成为大家在今后的开发过程中,Java编程手册的重要部分. 1. 把Strings转换成int和把int转换成String ...

  6. day1 函数 (独立功能代码块)

    1.引入函数 2.函数执行过程 4.带参数的函数 5.带返回值的函数 6. 多个返回值 (return a,b,c)元组 7.4种函数 1.引入函数 独立功能代码块 ---> 封装 ----&g ...

  7. js经常使用功能代码

    js经常使用功能代码(持续更新): 1---折叠与展开 <input id="btnDisplay" type="button" class=" ...

  8. html和js基础功能代码备份

    1)贴图:<img src="图片地址">2)加入连接:<a href="所要连接的相关地址">写上你想写的字</a> 3) ...

  9. 常见.NET功能代码汇总

    1,在Web上修改指定文件位置的Web.config 这里需要使用 WebConfigurationManager 类,但必须使用WebConfigurationFileMap类来指定文件位置,看代码 ...

  10. 手机开发必备技巧:javascript及CSS功能代码分享

    1. viewport: 也就是可视区域.对于桌面浏览器,我们都很清楚viewport是什么,就是出去了所有工具栏.状态栏.滚动条等等之后用于看网页的区域,这是真正有效的区域.由于移动设备屏幕宽度不同 ...

随机推荐

  1. 数据结构与算法JS实现

      行解算法题,没错,就是这么方便. 当然也可以使用 Node.js 环境来执行,具体参考Node.js官方文档即可. 二 对象和面向对象编程 js中5种数据类型,并没有定义更多的数据类型,但是运用j ...

  2. java学习之成员内部类

    //成员内部类:直接在类中定义 /*成员内部类的通常用法: * 通常是提供给外部类使用不进行内部类的实例化 * 因此一般把他设为私有的类用private限定 * * */ /*Demo*/ class ...

  3. Zabbix配置网络流量监控报警

    一.SNMP简单概述 1.什么是Snmp SNMP是英文"Simple Network Management Protocol"的缩写,中文意思是"简单网络管理协议&qu ...

  4. python itertools 模块

    Python的内建模块itertools提供了非常有用的用于操作迭代对象的函数 首先,我们看看itertools提供的几个“无限”迭代器: >>> import itertools ...

  5. Tensorflow object detection API 搭建属于自己的物体识别模型

    一.下载Tensorflow object detection API工程源码 网址:https://github.com/tensorflow/models,可通过Git下载,打开Git Bash, ...

  6. Redis的数据结构、通用操作及其特性

    Redis的数据结构 五种数据类型: 字符串(String).字符串列表(list).字符串集合(set).有序字符串集合(sorted set).哈希(hash) key定义的注意点: 不要过长,不 ...

  7. windows 系统验证是否为正版

    博客园里边写这种帖子,足以证明我有多无聊.话不多说,上干货. 一台计算器如果没有操作系统,就是一块大的板砖,拿起来抡人太重,放地上做床又太小. 如何查看自己操作系统呢?windows7 桌面找到我的电 ...

  8. 移动端与pc端如何用localStorage实现历史纪录?

    1.使用jq完成localStorage实现历史纪录版. 代码如下: <!DOCTYPE html> <html> <head lang="en"&g ...

  9. sql*loader以及oracle外部表加载Date类型列

    Oracle sqlldr LOAD DATAINFILE *INTO TABLE testFIELDS TERMINATED BY X'9'TRAILING NULLCOLS(    c2 &quo ...

  10. SSM-网站前台博客系统制作(1)---前台+Google的Kaptcha

    前提: 1天半时间简单自己手写了一下前端布局和后台验证码的基本工作,简要说明一下遇到的问题和收获吧. 这次基本就是前台设计(首页)+Kaptcha图片验证码(之前弄了一个reCaptcha验证码 但是 ...