ofstream-保存一个返回txt的函数
我正在尝试在 txt 文件中添加日期。但这并没有以可读的格式出现以供阅读。
有人可以帮助我吗?我将非常感激!,谢谢。
代码
保存:
//...code....///
ofstream myfile ("text.txt");
if (myfile.is_open())
{
myfile << "date: " << date_function();
myfile.close();
}
日期功能:
void example::date_function(){
//...code....///
clock = localtime(&attrib.st_atime);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", clock);
cout << buf;
}
I'm trying to add dates in a txt file. But this is not coming out in a readable format for reading.
Someone could help me? I would be very grateful!, Thank you.
code
SAVE:
//...code....///
ofstream myfile ("text.txt");
if (myfile.is_open())
{
myfile << "date: " << date_function();
myfile.close();
}
DATE FUNCTION:
void example::date_function(){
//...code....///
clock = localtime(&attrib.st_atime);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", clock);
cout << buf;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的日期函数的返回类型为
void
并且您正在将其插入到文件中。这没有什么意义,也许你想要更多这样的东西:Your date function has a return type of
void
and you are inserting it into the file. That makes little sense perhaps you want something more like this:date_function 的返回类型应该是字符串。
(顺便问一下,你在哪里定义了
buf
?它在你的问题中使用了,但我没有看到定义?)The return type of date_function should be string.
(By the way, where have you defined
buf
? It's used in your question, but I don't see the definition?)