2

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]);
}
Community
  • 1
  • 1
plato
  • 23
  • 3

3 Answers3

2

IF you are working on linux/unix platform, ctrl-z suspends the program, programme receives SIGSTOP not EOF. EOF is usually ctrl-D. Run stty -a command to set/change the terminal line settings. IF you must use ctrl-z change the stty settings.

Hope this helps.

Gulraize
  • 21
  • 1
1

Ctrl+Z sends the the suspend signal on most Linux terminals. On Linux, EOF is usually Ctrl+D.

Ctrl+Z is EOF on Windows.

ecatmur
  • 152,476
  • 27
  • 293
  • 366
0

Because on Linux, Ctrl+D is the EOF marker and not Ctrl+Z which will send SIGSTP to the process.

So you were basically asking your process to stop each time you pressed Ctrl+Z when what you really wanted was to enter the EOF character.

Fingolfin
  • 5,363
  • 6
  • 45
  • 66