简体   繁体   English

接受\r\n输入C程序

[英]Accepting \r\n input C program

I would like to ask how can I accept \r\n without changing it to \\r\\n , with fgets .我想问一下如何接受\r\n而不将其更改为\\r\\n ,使用fgets

I want the program to translate the \r\n to a newline character instead of printing it as a string.我希望程序将\r\n转换为换行符,而不是将其打印为字符串。

Current code:当前代码:

char buff[1024];
printf("Msg for server: ");
memset(buff, 0, sizeof(buff));
fgets(buff, sizeof(buff), stdin);
printf(buff);

Input:输入:

test\r\ntest2

The output I want:我想要的 output:

test
test2

My current output:我目前的 output:

test\r\ntest2

OP is typing in OP 正在输入

\ r \ n and wants that changed to a line-feed. \ r \ n并希望将其更改为换行符。

Process the input string looking for a \ , the start of an escape sequence.处理输入字符串以查找\ ,即转义序列的开头。

if (fgets(buff, sizeof buff, stdin)) {
  char *s  = buff;
  while (*s) {
    char ch = *s++; 
    if (ch == '\\') {
      switch (*s++) {
        case 'r': ch = '\r'; break; // or skip printing this character with `continue;`
        case 'n': ch = '\n'; break; 
        case '\\': ch = '\\'; break;  // To print a single \
        default: TBD();  // More code to handle other escape sequences.
      }
    }
    putchar(ch);
  } 

[Edit] I now suspect OP is inputting \ r \ n and not carriage return line feed . [编辑] 我现在怀疑 OP 正在输入\ r \ n而不是回车换

I'll leave the below up for reference.我将以下内容留作参考。


After fgets() , use strcspn())fgets()之后,使用strcspn())

if (fgets(buff, sizeof buff, stdin)) {
  buff[strcspn(buff, "\n\r")] = '\0';  // truncate string at the first of \n or \r
  puts(buff);  // Print with an appended \n
}  

You would need to replace de \r\n substring with a newline character:您需要将 de \r\n substring 替换为换行符:

Live demo现场演示

#include <stdio.h>
#include <string.h>

int main(void)
{
    char buff[1024];

    printf("Msg for server: ");
    fgets(buff, sizeof(buff), stdin);

    char *substr = strstr(buff, "\\r\\n"); //finds the substring \r\n

    *substr = '\n'; //places a newline at its beginning

    while(*(++substr + 3) != '\0'){ //copies the rest of the string back 3 spaces 
        *substr = substr[3];   
    } 
    substr[-1] = '\0'; // terminates the string, let's also remove de \n at the end

    puts(buff);
}

Output: Output:

test
test2

This solution will allow you to have other \ characters or "\n" and "\r" separated substrings along the main string, if that's a concern, only that particular substring will be replaced, everything else stays the same.此解决方案将允许您在主字符串中使用其他\字符或"\n""\r"分隔的子字符串,如果这是一个问题,只有特定的 substring 将被替换,其他一切都保持不变。

in your input string, "\r" have two characters: '\' & 'r'.在您的输入字符串中,“\r”有两个字符:“\”和“r”。 but '\r' is a single character.但 '\r' 是单个字符。 "\r\n" is a string of 4 byte while "\r\n" is a string of 2 byte. "\r\n" 是一个 4 字节的字符串,而 "\r\n" 是一个 2 字节的字符串。

if you have to do this, write a string replace function before gets如果必须这样做,请在获取之前编写一个字符串替换 function

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

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