Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shift + Enter vs Enter in console

Tags:

c

enter

shift

I am trying to catch all characters that I input via my stdin stream except EOF. I want to input a multi-line text: each line with \n at the end.

int getline(char s[])
{
    printf("Call-getline()\n");

    int c;
    int idx=0;

    while((c=getchar()) != EOF)
    {
        s[idx++] = c;
    }

    printf("\n-EOF found--\n");

    s[idx] = '\0';

    return idx;
}

I don't know how to get rid of the \n that I get when i press enter, and I was wondering if shif+enter vs enter alone makes any difference. I read about what it does in Microsoft Word: new paragraph vs newline.

like image 917
Cătălina Sîrbu Avatar asked Oct 15 '22 09:10

Cătălina Sîrbu


1 Answers

The answer Removing trailing newline character from fgets() input was linked in the comments, which show you the solution.

However, I want to point out one other thing here. A common way of ending the input is by pressing Ctrl+D, which will send the EOF to the program. Or at least most (all?) *nix terminals do. But that's a detail specific for the terminal you're using, so you have to read the documentation for your particular terminal.

I found this answer, which tells you about how to do it on Windows. Unfortunately, the answer is basically that you cannot do it in a good way.

like image 82
klutt Avatar answered Oct 20 '22 00:10

klutt