byt*_*uff 50 c c++ string time std
为什么返回的字符串ctime()有一个换行符(0x0A)作为最终字符?例如,这段代码:
#include <iostream>
#include <cstdlib>
int main(int argc, char* argv[])
{
time_t now;
time(&now);
char* time_str = ctime(&now);
std::cout << time_str << "why is this on a new line?" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
...产生以下输出:
$ ./time.exe
Wed Oct 23 14:52:29 2013
why is this on a new line?
$
Run Code Online (Sandbox Code Playgroud)
没什么大不了的; 我可以从字符串中删除最后一个字节,但为什么ctime()要把它放在首位呢?
Yu *_*Hao 47
根据C99理由,新线路存在是因为现有的做法,我认为这与历史原因的说法相同.
国际标准的基本原理 - 编程语言 - C§7.23.3.1
asctime函数尽管该功能的名称暗示了与从标准中删除ASCII依赖性的原则的冲突,但由于现有技术,该名称被保留.出于与现有做法相同的原因,未采用从字符串格式中删除换行符的提议.
这是谈论asctime,但由于ctime相当于asctime(localtime(timer)),所以同样的规则适用.
| 归档时间: |
|
| 查看次数: |
5198 次 |
| 最近记录: |