便携式方式确定平台的行分隔符 [英] Portable way to determine the platform's line separator

查看:130
本文介绍了便携式方式确定平台的行分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不同的平台使用不同的行分隔符方案(LF,CR-LF,CR,NEL,Unicode LINE SEPARATOR等)。 C ++(和C)运行时库通过将'\\\
'
转换为目标平台的本地新行编码,从而使大多数程序变得透明。但是如果你的程序需要确定实际使用的字节序列,你怎么可以移植?



我想出的最好的方法是:


  1. 在文本模式下使用'\\\
    '
    写一个临时文件,




  2. $ b

    这感觉kludgy。有没有办法,没有临时文件?我尝试了stringstreams,但运行时实际上不翻译'\\\
    '
    在这种情况下(这是有道理的)。运行时是否以其他方式公开此信息?

    解决方案

    我没有C / C ++专家,似乎是标准库中的任何东西,将直接给你的行分隔符。翻译由文本模式文件功能透明地处理。



    即使你觉得你的方法是kludgy,它可能是最简单和最可靠的,因为你真的测试使用和写出的行分隔符。并且是可移植的,因为您使用标准库函数来写入和读取文件。


    Different platforms use different line separator schemes (LF, CR-LF, CR, NEL, Unicode LINE SEPARATOR, etc.). C++ (and C) runtime libraries make a lot of this transparent to most programs, by converting '\n' to and from the target platform's native new line encoding. But if your program needs to determine the actual byte sequence used, how could you do it portably?

    The best method I've come up with is:

    1. Write a temporary file in text mode with just '\n' in it, letting the run-time do the translation.
    2. Read back the temporary file in binary mode to see the actual bytes.

    That feels kludgy. Is there a way to do it without temporary files? I tried stringstreams instead, but the run-time doesn't actually translate '\n' in that context (which makes sense). Does the run-time expose this information in some other way?

    解决方案

    I'm no C/C++ expert, but there doesn't appear to be anything in the standard library that will directly give you the line separator. The translation is handled transparently by the text-mode file functions.

    Even though you feel your approach is "kludgy", it is probably the simplest and most reliable, since you are really testing what line separator is used and written out. And is portable, since you are using standard library functions to write and read the file.

    这篇关于便携式方式确定平台的行分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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