简体   繁体   English

如何使用Visual Studio 6 C ++检测字符串中的换行符

[英]How to detect newline character(s) in string using Visual Studio 6 C++

I have a multi-line ASCII string coming from some (Windows/UNIX/...) system. 我有一个来自某些(Windows / UNIX / ...)系统的多行ASCII字符串。 Now, I know about differences in newline character in Windows and UNIX (CR-LF / LF) and I want to parse this string on both (CR and LF) characters to detect which newline character(s) is used in this string, so I need to know what "\\n" in VS6 C++ means. 现在,我知道Windows和UNIX中的换行符(CR-LF / LF)的差异,我想在两个(CR和LF)字符上解析这个字符串,以检测在这个字符串中使用了哪个换行符,所以我需要知道VS6 C ++中的“\\ n”是什么意思。

My question is if I write a peace of code in Visual Studio 6 for Windows: 我的问题是,如果我在Visual Studio 6 for Windows中编写代码的和平:

bool FindNewline (string & inputString) {
    size_t found;
    found = inputString.find ("\n");
    return (found != string::npos ? true : false);
}

does this searches for CR+LF or only LF? 这会搜索CR + LF还是仅搜索LF? Should I put "\\r\\n" or compiler interprets "\\n" like CR+LF? 我应该把“\\ r \\ n”或编译器解释为“\\ n”,如CR + LF吗?

inputString.find ("\n");

will search for the LF character (alone). 将搜索LF字符(单独)。

Library routines may 'translate' between CR/LF and '\\n' when I/O is performed on a text stream, but inside the realm of your program code, '\\n' is just a line-feed. 当在文本流上执行I / O时,库例程可以在CR / LF和'\\ n'之间“转换”,但在程序代码的范围内,'\\ n'只是一个换行符。

"\\n" means "\\n". “\\ n”表示“\\ n”。 Nothing else. 没有其他的。 So you search for LF only. 所以你只搜索LF。 However Microsoft CRT does some conversions for you when you read a file in text mode, so you can write simpler code, sometimes. 但是,当您在文本模式下读取文件时,Microsoft CRT会为您进行一些转换,因此您有时可以编写更简单的代码。

Apart from the VS6 part (you really, really want to upgrade this, the compiler is way out of date and Microsoft doesn't really support it anymore), the answer to the question depends on how you are getting the string. 除了VS6部分(你真的,真的想升级它,编译器已经过时了,微软不再支持它了),问题的答案取决于你如何获得字符串。

For example, if you read it from a file in text mode, the runtime library will translate \\r\\n into \\n. So if all your text strings are read in text mode via the usual file-based APIs, your search for 例如,如果您在文本模式下从文件中读取它,则运行时库会将\\r\\n转换为\\n. So if all your text strings are read in text mode via the usual file-based APIs, your search for \\n. So if all your text strings are read in text mode via the usual file-based APIs, your search for \\n` (ie, newline only) would be sufficient. \\n. So if all your text strings are read in text mode via the usual file-based APIs, your search for \\ n`(即仅限换行符)就足够了。

If the strings originate in files that are read in binary mode on Windows and are known to contain the DOS/Windows line separator \\r\\n , the you're better off searching for that character sequence. 如果字符串源自在Windows上以二进制模式读取的文件,并且已知包含DOS / Windows行分隔符\\r\\n ,则最好搜索该字符序列。

EDIT: If you do get it in binary form, yes, ideally you'd have to check for both \\r\\n and \\n . 编辑:如果你确实以二进制形式得到它,是的,理想情况下你必须检查\\r\\n\\n However I would expect that they aren't mixed within one string and still carry the same meaning unless it's a really messed up data format. 但是我希望它们不会在一个字符串中混合,并且仍然具有相同的含义,除非它是一个非常混乱的数据格式。 I would probably check for \\r\\n first and then \\n second if the strings are short enough and scanning them twice doesn't make that much of a difference. 如果字符串足够短并且扫描它们两次并没有产生太大的差别,我可能会先检查\\r\\n然后再检查\\n秒。 If it does, I'd write some code that checks for both \\r\\n and single \\n in a single pass. 如果是这样,我会写一些代码,在一次通过中检查\\r\\n和单\\n

All translation between "\\n" and "\\r\\n" happens during I/O. “\\ n”和“\\ r \\ n”之间的所有转换都发生在I / O期间。 At all other times, "\\n" is just that and nothing more. 在所有其他时间,“\\ n”就是这样,仅此而已。

Somehow: return (found != string::npos ? true : false); 不知何故: return (found != string::npos ? true : false); reminds me of another answer I wrote a while back. 让我想起我前一段时间写的另一个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM