如何在C++中确定环境换行符1?谷歌为C#和.NET提供了许多结果,但我没有看到任何方法为非CLI C++做到这一点.
附加信息:我需要扫描一个const char*字符.
1 "环境换行"我的意思是\r\n在Windows,\nLinux和\rMac上.
std::endl插入适合系统的换行符.您可以使用a ostringstream在运行时将换行符序列确定为字符串.
#include <sstream>
int main()
{
std::ostringstream oss;
oss << std::endl;
std::string thisIsEnvironmentNewline = oss.str();
}
Run Code Online (Sandbox Code Playgroud)
编辑:*请参阅下面的评论,为什么这可能不起作用.
如果您知道您的平台将仅限于Windows,Mac和Unix,那么您可以使用预定义的编译器宏(此处列出)在编译时确定结束序列:
#ifdef _WIN32
#define NEWLINE "\r\n"
#elif defined macintosh // OS 9
#define NEWLINE "\r"
#else
#define NEWLINE "\n" // Mac OS X uses \n
#endif
Run Code Online (Sandbox Code Playgroud)
大多数非Windows和非Apple平台都是使用的某种Unix变体\n,因此上述宏应该可以在许多平台上运行.唉,我不知道在编译时为所有可能的平台确定最终序列的任何可移植方法.