将当前时间输出到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. MVC5+EF6 完整教程17--升级到EFCore2.0(转)

    MVC5+EF6 完整教程17--升级到EFCore2.0 2017年08月22日 14:48:12 linux12a 阅读数:2814   EF Core 2.0上周已经发布了,我们也升级到core ...

  2. js中的this指向问题(小计)

    this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定,this最终指向调用它的对象. 1.函数调用模式 当一个函数并非一个对象的属性时,那么它就是被当做函数来调用的.在此种模式下,t ...

  3. cycle标签和random两种方式美化表格

    一:cycle标签实现给表格变色 1. <style>标签里写好需要的颜色 2. 在要变色的地方(行/列)加固定的语句,按照顺序依次执行 代码: <!DOCTYPE html> ...

  4. 在sql中case子句的两种形式

    case子句,在select后面可以进行逻辑判断. 两种形式:判断相等.判断不等 一.判断相等的语法: case 列名 when ...  then ... when ...  then ... el ...

  5. Delphi使用两种不同方法获取系统端口信息--(装载)

    Delphi使用两种方法获取windows系统的端口,还可测试发送消息,点击获取端口信息后,可依次得到如下信息:DCB结构大小.波特率大小.XON的临界值.XOFF的临界值.字符位数.奇偶检验位.停止 ...

  6. 软件工程实践助教每周小结 < 福州大学 | 傅明建 >

    第一周助教小结 1. 助教博客链接: http://www.cnblogs.com/sinceway/ 2. 本周点评的作业数量:约22份,有多次交互 3. 本周点评有困难的作业链接: https:/ ...

  7. babel 7.x 和 webpack 4.x 配置vue项目

    很偶然的今天想开个自己的小项目,记录一下最近项目工程上实现的一个小交互.按照之前运行非常流畅的配置走一遍,打包遇到各种坑.只好根据命令行的报错逐个排查,发现babel升级了一个大版本,已经到7.x了. ...

  8. winform Combobox出现System.Data.DataRowView的解决的方法

    个人总结: 1.触发了SelectedIndexChanged事件时:comboBox1.DataSource = dt;要放在comboBox1.SelectedIndex = 0;的上面 comb ...

  9. 针对多条件查询,应对 url 无用 null 值现象处理

    多条件查询 应对 url 无用 null 值现象 处理例如:http://xxoo.b2b.com/orders?city_id=5&repertory_id=7&area_id=39 ...

  10. python 字典嵌套字典赋值异常

    针对dict中 嵌套dict 出现复制异常 lists={} test=['s1','s2','s3'] data = {'value': '',} for i in range(2): lists[ ...