本文介绍了从asctime()中删除换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想摆脱时间之后asctime()给我的新行

I want to get rid of the new line that asctime() gives me after the time

string str;
time_t tt;
struct tm * ti;
time(&tt);
ti = localtime(&tt);
str = asctime(ti);
for (int i = 0; i < 2; i++)
    cout << str;

输出为

fri apr 26 00:59:07 2019
fri apr 26 00:59:07 2019

但是我想要这种形式

fri apr 26 00:59:07 2019fri apr 26 00:59:07 2019

推荐答案

由于换行符是最后一个字符,因此您要做的就是删除字符串中的最后一个字符.您可以通过调用pop_back

Since the newline is the final character, all you have to do is remove the final character in the string. You can do this by calling pop_back

str.pop_back();

在Windows上,换行符为\r\n BUT \n是C和C ++的与平台无关的换行符.这意味着当您在文本模式下将\n写入stdout时,字符将被转换为\r\n.您只需在任何地方使用\n,您的代码将跨平台.

On Windows, a newline is \r\n BUT \n is the platform independent newline character for C and C++. This means that when you write \n to stdout in text mode the character will be converted to \r\n. You can just use \n everywhere and your code will be cross platform.

这篇关于从asctime()中删除换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 09:14