c++写日记簿程序
时间: 2023-09-14 19:15:54 浏览: 151
好的,下面是一个简单的C++日记簿程序实现:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;
int main()
{
string filename = "diary.txt";
string content;
time_t now = time(0);
tm* ltm = localtime(&now);
int year = ltm->tm_year + 1900;
int month = ltm->tm_mon + 1;
int day = ltm->tm_mday;
int hour = ltm->tm_hour;
int minute = ltm->tm_min;
int second = ltm->tm_sec;
cout << "请输入日记内容:" << endl;
getline(cin, content);
ofstream outfile;
outfile.open(filename, ios::app);
outfile << year << "-" << month << "-" << day << " " << hour << ":" << minute << ":" << second << endl;
outfile << content << endl;
outfile << "----------------------------------------" << endl;
outfile.close();
cout << "日记已保存成功!" << endl;
return 0;
}
```
这个程序会提示用户输入日记内容,然后获取当前时间,最后将时间和内容写入到文件“diary.txt”中。
阅读全文