C++标准库获取时间、简单的文件操作
来源:互联网 发布:阿里云os系统官网 编辑:程序博客网 时间:2024/05/24 04:45
参考文章:
http://blog.csdn.net/luotuo44/article/details/46854229
http://www.2cto.com/kf/201404/290706.html
http://www.cplusplus.com/reference/iomanip/put_time/
http://blog.csdn.net/u010177286/article/details/50353464
http://blog.csdn.net/wangjieest/article/details/7761051
http://codereview.stackexchange.com/questions/124395/c-time-types-and-format-conversions 这个借鉴的是put_time()转string
只是在以上的一堆,借鉴了一个简单的用C++标准库的日期及其格式化程序。
程序如下:(QT下面写的)
#include <chrono>#include <ctime>#include <string>#include <iomanip>#include <iostream>#include <fstream>#include <sstream>using namespace std::chrono;using namespace std;int main(int argc, char *argv[]){ system_clock::time_point now = system_clock::now(); std::time_t time = system_clock::to_time_t(now); std::stringstream ss; //把时间数据格式转换为字符串 ss<<std::put_time(std::localtime(&time),"%Y-%m-%d %H:%M:%S "); cout<<ss.str()<<endl; ofstream wfile("data-"+ss.str()+".txt"); if(wfile.is_open()){ wfile<<"hello wk!!!"; //往文件中写数据 cout << "file open successed."; } else cout<<"file open failed!"; // auto t = chrono::system_clock::to_time_t(std::chrono::system_clock::now()); // cout<< std::put_time(std::localtime(&t), "%Y-%m-%d %X")<<endl; // cout<< std::put_time(std::localtime(&t), "%Y-%m-%d %H.%M.%S")<<endl; return 0; }
但是,会报错,put_time()不是标准库中的函数,同样的还有get_time().
查阅资料显示,put_time()、get_time()在gcc5中才实现,gcc4中没有,
而ubuntu14.04默认安装的是gcc4.8,太老了:http://stackoverflow.com/questions/14136833/stdput-time-implementation-status-in-gcc
安装gcc5,并设置,参照http://blog.sina.com.cn/s/blog_54dd80920102vvt6.html
错误:
ofstream data_file("datarecord-"+ss.str()+".txt");
。。。。。。。。。has initializer but incomplete type
原因:没有包含操作文件的头文件#include<fstream>
- C++标准库获取时间、简单的文件操作
- [C++STDlib基础]关于日期时间的操作——C++标准库头文件<ctime>
- C 标准库获取当前系统时间
- 标准C文件操作
- C语言标准库之文件操作
- C标准库之文件IO操作
- C标准库文件和文件夹操作
- 简单的时间获取C/S程序
- Windows 获取文件操作时间的方法
- js获取时间戳以及对时间的简单操作
- linux c 获取文件的时间信息
- Linux下C编程-----文件操作(1) 通过系统调用简单操作标准输入、标准输出、标准错误
- 【ThinkingInC++】48、用标准C库中的时间函数生成简单的Time类
- 获取当前时间---使用标准C
- 标准C获取当前系统时间
- [C++STDlib基础]关于C标准输入输出的操作——C++标准库头文件<cstdio>
- ANSI C提供的文件操作标准库函数
- 标准C语言中的文件操作函数的内存版
- Matlab基础之单元数组和结构数组
- java中StringTokenizer类的用法
- 7天入门php-session基础使用及预定义常量
- 【设计模式系列】--抽象工厂
- mxnet window10 cpu 模式的安装
- C++标准库获取时间、简单的文件操作
- ps
- (Java)LeetCode-53. Maximum Subarray
- python 学习笔记(3) 各种数据类型常用的函数
- 线段树求区间最大值
- shell详解二
- Singleton单例模式
- StringBuffer的常用方法
- 栈的链式存储API