您的位置:首页 > 编程语言 > C语言/C++

C++向文件中写入数据

2014-12-30 18:43 141 查看
#include<iostream>

#include<fstream>

#include<ctime>

using namespace std;

int main()

{

time_t time_now;
//定义一个time_t结构的对象

time(&time_now);
//获取系统当前的日历时间

tm timep;
//时间结构体

localtime_s(&timep,&time_now);
//将日历时间转换为本地时间存储在tm结构体对象中

ofstream fileOpen;
//定义ofstream 对象

char path[200],FilePath[2000];

sprintf_s(path,200,"Log\\%d年%d月.txt",timep.tm_year+1900,timep.tm_mon);//注意Log文件夹必须已经存在

//否则将导致文件打不开

fileOpen.open(path,ofstream::app);

if(!fileOpen)

{

cout<<"没有打开文件!"<<endl;

fileOpen.close();

return 0;

}

char buf[10]={'I','L','o','v','e','y','o','u','!'};

sprintf_s(FilePath,2000,"%d/%d/%d %d:%d:%d %s \n",

timep.tm_year+1900,

timep.tm_mon+1,

timep.tm_mday,

timep.tm_hour,

timep.tm_min,

timep.tm_sec,

buf);

fileOpen<<FilePath;

fileOpen.close();

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: