在C ++ / Qt中获取平台特定的行尾字符 [英] Get platform specific end of line character in C++/Qt

查看:218
本文介绍了在C ++ / Qt中获取平台特定的行尾字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么东西为任何平台获得正确的行尾符号吗?我的意思是,如果我想写EOL到文件,我可以使用 \\\
Windows和Unix,但也有 \r\



因此,我需要一些类似于 Environment.NewLine 在C#中,它应该是一些类,而不是技巧与 #ifdef Q_OS_WIN32 ...



用例是从 QTextStream 读取所有数据并用新行拆分。无论如何,如果 QTextStream QString :: split 足够聪明地处理 \\\
正确地在任何平台上,我想知道我提出的问题。

解决方案

文件在文本模式下,\\\
字符应重新解释为适合该系统的任何内容。对于Windows,这意味着CRLF(回车,换行),在Unix上,它只是LF单独,而Macintosh标准是一个CR本身。



读取,准备好在这些字符中的任何一个结束行,但是如果您发现回车,请检查是否紧跟其后有换行符,如果有,请考虑它是同一行的一部分。 / p>

Is there anything for getting right end-of-line symbol for any platform? I mean, I can use \n for Windows and Unix if I want to write EOL to file, but there is also \r\n and this would be significant if I'll do searching in binary data.

So, I need something like Environment.NewLine in C# and it should be some class and not trick with #ifdef Q_OS_WIN32....

Use case is reading from QTextStream all data and split it by new line. Anyway, if QTextStream or QString::split is smart enough to handle \n correctly on any platform, I want to know about thing I asked.

解决方案

When you are writing a file in text mode, the "\n" character should be reinterpreted as whatever is appropriate for that system. For Windows, that means CRLF (carriage return, line feed), on Unix, it's just LF alone, and the Macintosh standard is a CR by itself.

When you are reading, be ready to end a line at either one of those characters, but if you find a carriage return, check to see if there is a line feed immediately after it, and if there is, consider it part of the same line.

这篇关于在C ++ / Qt中获取平台特定的行尾字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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