C++实现的一些功能代码

将当前时间输出到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 + 1900 << "-" << local_time.tm_mon + 1 << "-" << 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 1;
}

输出的结果:

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

#include <iostream>
#include <string>
using namespace std;

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

    if (4 != argc)
    {
        cout << "input params error" << endl;
        return -1;
    }

    string type = argv[1];
    string cutmethod = argv[2];
    string resolution = argv[3];

    cout << type << "_" << cutmethod << "_" << resolution << endl;
    

    system("pause");
    return 1;
}

输出结果:

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

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

猜你喜欢

转载自www.cnblogs.com/k7k8k91/p/10323007.html