首页 > 编程语言 > 详细

c++|文件操作以及时间表达

时间:2021-07-19 14:37:41      阅读:11      评论:0      收藏:0      [点我收藏+]
 
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <fstream>
#include <sstream>
#include<time.h>
using namespace std;

int main()
{
    /*以写的方式打开info.txt文件,若存在info.txt文件会覆盖其中的原有单词
    如没有info.txt文件,则此代码会创建这样一个txt文件*/
        ofstream out("info.txt", ios::out | ios::trunc);
        if (!out.is_open()) {
          cout << "存储失败! " << endl;
        }
        string word[] = {"add", "right", "left", "fft"};
        for (int i = 0; i < 4; i++) {
          out << word[i] << "\n";
        }            
        out.close(); /*关闭输入文件*/

        //-----时间表示-----
        time_t timep;
        struct tm *p;
        time(&timep);
        p = gmtime(&timep);
        int c = 1900 + p->tm_year;

        // std::ostringstream ss ;
        // sprintf_s(current_time, "%d", c);
        // current_time=str(c);
        string current_time =
            to_string(1900 + p->tm_year) + "-" + to_string(1 + p->tm_mon) +
            "-" + to_string(p->tm_mday) + "_" + to_string(8 + p->tm_hour) +
            ":" + to_string(p->tm_min) + ":" + to_string(p->tm_sec) + ".txt";
        cout << current_time;

        return 0;
}

 

c++|文件操作以及时间表达

原文:https://www.cnblogs.com/polipolu/p/15029498.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!