std :: endl在字符串变量中? [英] std::endl in a string variable?

查看:97
本文介绍了std :: endl在字符串变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在字符串中保存多行.我有一个字符串logstring,我想保存多重播放错误日志,以后可以在txt文件或控制台输出中打印这些日志.是否可以使用endl格式化字符串变量?我搜索了互联网,但只找到

Hi i want to save multiply lines in a string. I got a string logstring and i want to save multiplay error logs which i later can print in a txt file or as a console output. Is there a possibility to use endl to format a string variable? I searched the internet but i only find

cout << "" << endl;

现在我的想法是:

std::string logstring;
logstring = logstring + "Error Message" + "Number" + "Time + Date";
logstring += endl;

是否可能出现这种情况,或者是否无法格式化字符串变量?稍后我想将它们打印到log.txt文件中?可以使用这样的字符串吗?

Is something like this possible or is there no way to format string variables? Later i want to print them to a log.txt file? Is it possible to use a string like this?

std::ofstream logfile;
logfile.open(log.txt);
logfile << logstring;

文本文件应如下图所示

Error Message 1 Date
Error Message 2 Date
Error Message 3 Date
...

是否可能像这样得到它?还是我必须单独打印所有行?

Is it possibly to get it like this or do i have to print all lines seperatly?

推荐答案

别忘了std::endl添加新行 并刷新缓冲区.

Don't forget std::endl adds a new line and flushes the buffer.

如果只需要换行\n,请使用+将其添加到字符串中,或​​使用<< '\n'将其流式传输到流中.

If you simply want new lines, \n, add them to your string, using + or stream them to a stream, using << '\n'.

例如,

std::string logstring;
logstring = logstring + "Error Message" + "Number" + "Time + Date";
logstring += '\n';

这篇关于std :: endl在字符串变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆