C++向文件中写入数据

#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;
}

猜你喜欢

转载自blog.csdn.net/u012782049/article/details/42269917
今日推荐