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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With