C++ 字符串中的回车和换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17522791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:15:46  来源:igfitidea点击:

C++ Carriage return and line feed in a string

c++stringcarriage-returnlinefeed

提问by user1728363

I am working with the communication for some TCP/IP connected equipment in C++. The equipment requires that the commands sent are ended with \r\n.

我正在使用 C++ 处理一些 TCP/IP 连接设备的通信。设备要求发送的命令以\r\n 结束。

I am using a configuration file from which I am reading the commands used in the communication.

我正在使用一个配置文件,从中读取通信中使用的命令。

The problem I have is that the commands \r\n are interpreted as the 4 characters they are and not as carriage return and line feed.

我遇到的问题是命令 \r\n 被解释为它们的 4 个字符,而不是作为回车和换行符。

I have tried to use the string.data() but I get the same result as string.c_str().

我曾尝试使用 string.data(),但得到的结果与 string.c_str() 相同。

Is there any nice function to get it correct from the beginning or do I have to solve this with a normal replace function? Or some other way that I have not thought about?

有没有什么好的功能可以从一开始就让它正确,还是我必须用正常的替换功能来解决这个问题?或者其他一些我没有想过的方式?

I guess, if I don't find a really neat way to do this I will just omitt the \r\n in the configuration file and add it afterwards, but it would be nice to have it all in the configuration file without any hard coding. I think I would need to do some hard coding as well if I would try to replace the four characters \r\n with their correct characters.

我想,如果我没有找到一个非常巧妙的方法来做到这一点,我只会在配置文件中省略 \r\n 并在之后添加它,但是如果没有任何困难,将它全部放在配置文件中会很好编码。我想如果我尝试用正确的字符替换四个字符 \r\n ,我还需要做一些硬编码。

Thanks for any help

谢谢你的帮助

Edit: The config file contains lines like this one.

编辑:配置文件包含这样的行。

MONITOR_REQUEST = "TVT?\r\n"

MONITOR_REQUEST = "TVT?\r\n"

回答by James Kanze

If the data in the configuration file requires translation, you have to translate it. Short of regular expressions (which are clearly overkill for this), I don't know of any standard function which would do this. We use something like:

如果配置文件中的数据需要翻译,则必须进行翻译。缺少正则表达式(这显然是矫枉过正),我不知道有任何标准函数可以做到这一点。我们使用类似的东西:

std::string
globalReplace(
    std::string::const_iterator begin,
    std::string::const_iterator end,
    std::string const& before,
    std::string const& after )
{
    std::string retval;
    std::back_insert_iterator<std::string> dest( retval );
    std::string::const_iterator current = begin;
    std::string::const_iterator next
            = std::search( current, end, before.begin(), before.end() );
    while ( next != end ) {
        std::copy( current, next, dest );
        std::copy( after.begin(), after.end(), dest );
        current = next + before.size();
        next = std::search( current, end, before.begin(), before.end() );
    }
    std::copy( current, next, dest );
    return retval;
}

for this. You could call it with "\\r\\n", "\r\n"for the last to arguments, for example.

为了这。例如,您可以使用"\\r\\n", "\r\n"for last to 参数调用它。

回答by Tony Delroy

Sounds like your configuration file actually contains 4 distinct characters '\', 'r', '\', and 'n'... you must either change the file (e.g. by using actual line endings in the file to encode line endings in the strings, and even then some systems won't happen to use \r\n as their text file line delimiters), or do some replace/translation as you suggest. The translation is the more portable approach. It can also be pretty fast... just maintain two pointers/iterators/indices into the string - one for the read position and one for the write, and copy across while compacting the escape notation.

听起来您的配置文件实际上包含 4 个不同的字符'\', 'r','\''n'... 您必须更改文件(例如,通过使用文件中的实际行尾对字符串中的行尾进行编码,即使这样,某些系统也不会发生使用 \r\n 作为他们的文本文件行分隔符),或者按照您的建议进行一些替换/翻译。翻译是更便携的方法。它也可以非常快......只需在字符串中维护两个指针/迭代器/索引 - 一个用于读取位置,一个用于写入,并在压缩转义符号的同时复制。

回答by KK.

You need not had code anything in code. You can very well read the characters to append to outgoing string from the same configuration file.

您不需要在代码中编写任何代码。您可以很好地从同一配置文件中读取要附加到传出字符串的字符。

Assuming that you have name value pair in your conf file :

假设您的 conf 文件中有名称值对:

APPEND_EOL="\n\r" str_to_send1="this is a test%%APPEND_EOL%%"

APPEND_EOL="\n\r" str_to_send1="这是一个测试%%APPEND_EOL%%"

while parsing the line before sending you can generate the actual line. In case you can't store the APPEND_EOL in the same line, then you can pick it up from ENV with a default in case it is not defined, or, maybe in another config file of yours.

在发送之前解析行时,您可以生成实际行。如果您不能将 APPEND_EOL 存储在同一行中,那么您可以使用默认值从 ENV 中提取它,以防它未定义,或者,也许在您的另一个配置文件中。