Possible Duplicate:
What is EOF in the C programming language?
When I was learning K&R C programming language, I'm trying to write a program which reads some lines from console then output all of the input. I need to use ctrl-z (EOF) to terminate my input stream. But the problem is when I am tapping ctrl-z, the program stopped, all the inputs can't be outputed to console (the gdb says "Program received signal SIGTSTP, Stopped(user). 0X00132416 in __kernel_vsyscall()"). I tried to use kill function to handle this terminating signal but failed. What can I do to make the console output all the input lines. part of my codes are as follows:
#include<stdio.h>
#include<string.h>
#define MAXLINES 5000 /* possibly max amount of lines */
char *lineptr[MAXLINES]; /* pointers to lines read */
int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);
main(int argc, char *argv[])
{
int nlines; /* number of lines read */
**if ((nlines = readlines(lineptr, MAXLINES)) >= 0)** {//program finished when I tap ctrl-z
writelines(lineptr, nlines); // from this line can't be run
return 0;
} else {
printf("input too big to sort\n");
return 1;
}
}
#define MAXLEN 1000
int get_line(char *, int);
char *alloc(int);
/* readlines : read the input */
int readlines(char *lineptr[], int maxlines)
{
int len, nlines;
char *p, line[MAXLEN];
nlines = 0;
while ((len = get_line(line, MAXLEN)) > 0)
if (nlines >= maxlines || (p = alloc(len)) == NULL)
return -1;
else {
line[len - 1] = '\0';
strcpy(p, line);
lineptr[nlines++] = p;
}
return nlines;
}
/* writelines : output the readlines */
void writelines(char *lineptr[], int nlines)
{
int i;
for (i = 0; i < nlines; i++)
printf("%s\n", lineptr[i]);
}