5

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.

klutt
  • 30,332
  • 17
  • 55
  • 95
Cătălina Sîrbu
  • 1,253
  • 9
  • 30
  • This answer: [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) shows how to remove the final newline from a string. It doesn't matter that you didn't use `fgets`, and is safe when there isn't a newline there. So that would be `s[strcspn(s, "\n")] = 0;` – Weather Vane Oct 31 '19 at 12:30
  • Yes, it is my mistake. There should only be `s[idx] = '\0'` because when the loop exits, the `idx` is already incremented by one. – Cătălina Sîrbu Oct 31 '19 at 12:37
  • @WeatherVane , `s[--idx] = '\0'` won't work always as the codeblocks console will always push a newline in any input stream when you press `enter`? – Cătălina Sîrbu Oct 31 '19 at 12:39
  • That overwrites the `enter` (newline). But if it isn't there, it either overwrites a character you want, or if the there wasn't any input, indexs `s[-1]` which is bad. But the comment I was referring to has been deleted, although it correctly identified a problem with `s[++idx] = '\0';` – Weather Vane Oct 31 '19 at 12:40
  • I don't know. Windows console does not respond to Ctrl-Enter, and Shift-Enter does the same as Enter. Enter is *already* a "control character", which can be typed as Ctrl-M. – Weather Vane Oct 31 '19 at 12:50
  • Oh, i got it. I also edit my question. Also, i wanted to know if there is any difference of sending the string using `ctrl+enter` or `enter alone`. How are they interpreted different by the console. Is any difference? I can't find any. – Cătălina Sîrbu Oct 31 '19 at 12:51
  • 1
    Your `getline` function is unsafe because it does not check array bounds. – Ian Abbott Oct 31 '19 at 12:51
  • Thank you! So there is no difference. – Cătălina Sîrbu Oct 31 '19 at 12:52
  • @IanAbbott you are totally right, but i have my `char s[100]` in my main function and i take care always to input less or equal characters from my keyboard! This example's purpose is learning only! – Cătălina Sîrbu Oct 31 '19 at 12:53

2 Answers2

3

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.

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
klutt
  • 30,332
  • 17
  • 55
  • 95
  • I am using Windows, so i tried with `CTRL + Z` on my last line it doesn't work. Ex:`Here is the text as if i was to input and so forth and here i put ^Z ` and afterwords i press enter. Nothing happends, it just go to the next line – Cătălina Sîrbu Oct 31 '19 at 12:43
  • 1
    On Windows, Ctrl-Z only works immediately after Enter was pressed, and needs another Enter too. – Weather Vane Oct 31 '19 at 12:46
  • So there is normal that it won't works as long as `Ctrl-Z` is not the first element of the line followed by key press `ENTER` – Cătălina Sîrbu Oct 31 '19 at 12:47
0

If I'm not mistaken, we can press CTRL + C to exit on visual studio and Dev C++ console otherwise if we talk about CMD then you should write exit followed by an Enter

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
tirmazi2
  • 1
  • 1