4

I would like to ask how can I accept \r\n without changing it to \\r\\n, with fgets.

I want the program to translate the \r\n to a newline character instead of printing it as a string.

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:

test
test2

My current output:

test\r\ntest2
anastaciu
  • 23,467
  • 7
  • 28
  • 53
Jerry
  • 114
  • 2
  • 13
  • 1
    You need to substitute these four characters "\r\n" for the new line character. – Vlad from Moscow Feb 24 '21 at 11:32
  • 1
    `fgets` doesn’t transform the input, it fills the buffer with the exact data it gets. If you want to modify the output, you’ll need to do that yourself. – Konrad Rudolph Feb 24 '21 at 11:32
  • You don't need to zero your buffer before using it like that. You can just let `fgets` go for it. – tadman Feb 24 '21 at 11:40
  • How do you display the output? If that's the debugger view, it will show control characters using that notation. – tadman Feb 24 '21 at 11:41
  • `accept \r\n without changing it to \r\n` is not very clear. *what* is changed? – wildplasser Feb 24 '21 at 11:47
  • Does this answer your question? [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input) – Rob Feb 24 '21 at 11:54
  • 1
    Jerry, when you write "Input: test\r\ntest2", is that 14 keys? `t, e, s, t, \, r, \, n, t, e, s, t, 2, enter`? If not, what? – chux - Reinstate Monica Feb 24 '21 at 11:57
  • @Rob I have read that question before, and sadly it does not answer my question. – Jerry Feb 24 '21 at 12:03
  • 1
    @chux-ReinstateMonica yes, I typed in that 14 keys. Apologies, I read the your question wrongly. – Jerry Feb 24 '21 at 12:04
  • @wildplasser Apologies, apparently I used the wrong amount of backslash thus you only see \r\n. I have made some changes to the question. – Jerry Feb 24 '21 at 12:12
  • @tadman I guess the 0 does not really affects anything, at most some computing power. I want to accept user input for a http request. E.g GET / HTTP/1.1\r\nHost: Example.com\r\n\r\n – Jerry Feb 24 '21 at 12:14
  • @KonradRudolph Yes, I am looking for how to do it. I have tried a few methods, but none of them work. – Jerry Feb 24 '21 at 12:16
  • @Jerry Alright, that makes perfect sense. I misunderstood your question as being puzzled that `fgets` doesn’t perform the conversion automatically. – Konrad Rudolph Feb 24 '21 at 13:49

4 Answers4

5

OP is typing in

\ r \ n and wants that changed to a line-feed.

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);
  } 
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
2

[Edit] I now suspect OP is inputting \ r \ n and not carriage return line feed.

I'll leave the below up for reference.


After fgets(), use 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
}  
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
2

You would need to replace de \r\n substring with a newline character:

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:

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.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
0

in your input string, "\r" have two characters : '\' & 'r'. but '\r' is a single character. "\r\n" is a string of 4 byte while "\r\n" is a string of 2 byte.

if you have to do this, write a string replace function before gets

Paul Yang
  • 346
  • 2
  • 9