I'm trying to comprehend Example 1.9 from the K&R book, but I don't get how to send EOF. Some sources mentioned Ctr+Z, but that simply terminates the program. I somehow managed to send EOF with a combination of Enter and Ctrl+Z and maybe Ctrl+V, but I can't reproduce it.
#include <stdio.h>
#define MAXLINE 1000
main()
{
int len;
int max;
char line[MAXLINE];
char save[MAXLINE];
max = 0;
while((len = getline_my(line, MAXLINE)) > 0)
if(len > max) {
max = len;
copy(line, save);
}
if(max > 0)
printf("%s", save);
}
getline_my(s, lim)
char s[];
int lim;
{
int c, i;
for(i=0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; i++)// As long as the condition is fulfilled
s[i] = c;
if (c == '\n') {
s[i] = c;
i++;
}
s[i] = '\0';
return(i);
}
copy(s1, s2)
char s1[];
char s2[];
{
int i;
i = 0;
while((s2[i] = s1[i]) != '\0')
i++;
}
the “end-of-file” (EOF) key combination can be used to quickly log out of any terminal. CTRL-D is also used in programs such as “at” to signal that you have finished typing your commands (the EOF command). key combination is used to stop a process. It can be used to put something in the background temporarily.
eof is not a signal but is implemented by the tty driver as a read of length 0 when you type ctrl-d .
The EOF in C/Linux is control^d on your keyboard; that is, you hold down the control key and hit d. The ascii value for EOF (CTRL-D) is 0x05 as shown in this ascii table . Typically a text file will have text and a bunch of whitespaces (e.g., blanks, tabs, spaces, newline characters) and terminate with an EOF.
Introduction. In Linux and MacOS environments, you can terminate standard input by outputting EOF (end of file) using the CTRL-D keyboard shortcut. In Windows, the CTRL-D key combination does not do the same.
You can simulate EOF with CTRL+D
(for *nix) or CTRL+Z
then Enter (for Windows) from command line.
In widows, when you are ready to complete the input, press the Enter
key and then press Ctrl+Z
and then Enter
to complete the input.
int main(){
char ch[100];
scanf("%[^EOF]",ch);
printf("\nthe string is:\n%s\n",ch);
fflush(stdin);
return 0;
}
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