1

Lets say I have the below C code:

int getLine (char line[])
{
    int c, i=0;
    while( (c=getchar()) != EOF )
           line[i++]=c;
    line[i++] = c;
    return i;
}

>> Enter: 007
>> ^Z
>> Output: 

If we closely observe the way I give output above, I am pressing Enter before stimulating EOF. This means, the length of string is 4 not 3 (excluding EOF).

When I am doing my exercises, I am really facing some trouble with that extra \n.

How do I stimulate EOF without newline? Is it possible at all?

>> Enter: 007^Z
>> ^Z
>> Output: length=6
Surya
  • 4,824
  • 6
  • 38
  • 63
  • 5
    On which platform? On Unix and derivatives, you would type the EOF 'character' twice — usually control-D rather than control-Z, though. That may also work on Windows; I don't know, but it is worth a try. (On Unix, control-D makes the data on the line available to the program. The first control-D gives it what you've typed already; the second gives it zero bytes to read, which is the indication of EOF.) – Jonathan Leffler Jan 07 '13 at 16:02
  • 1
    You should not save `EOF` in a `char` since `EOF` is negative and `char` could be `unsigned`. – effeffe Jan 07 '13 at 16:06
  • on my shell Linux its working `~$ ./try 07070707:~$ ` as commented by Mr. Jonathan Leffler – Grijesh Chauhan Jan 07 '13 at 16:11
  • @JonathanLeffler I think your comment should be an answer. – effeffe Jan 07 '13 at 16:12
  • 2
    `EOF` is not a character, it's a condition. Imagine you ask your daughter for a banana and she says: "There are no bananas". The sentence "There are no bananas" is not a banana. – pmg Jan 07 '13 at 16:17

2 Answers2

2

Converting my comment into an answer:

On which platform? On Unix and derivatives, you would type the EOF 'character' twice — usually control-D rather than control-Z, though. That may also work on Windows; I don't know, but it is worth a try.

(A response comment affirms that the platform is Windows.)

On Unix, control-D makes the data on the line available to the program. The first control-D gives it what you've typed already; the second gives it zero bytes to read, which is the indication of EOF.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Any references about it? I indeed find it that two `^d` can work like Enter in terminal and make me so puzzled. – Tony Aug 21 '14 at 03:58
  • I've not formally verified that they cover it, but I'd expect Stevens (et al) [Advanced Programming in the Unix Environment](http://www.amazon.com/Programming-Environment-Addison-Wesley-Professional-Computing/dp/0321637739/) or Rochkind [Advanced Unix Programming](http://www.amazon.com/Advanced-UNIX-Programming-Marc-Rochkind/dp/0131411543/) to cover it, and so might other Unix or Linux programming books, usually in the chapter on terminal handling. – Jonathan Leffler Aug 21 '14 at 05:46
1

Then avoid storing the newline, in the loop. It's not as if you're being forced to store all characters regardless of value. :)

Also, you're not terminating the string correctly. This:

line[i++] = c;

should be:

line[i] = '\0';

And of course, it's sensitive to buffer overflow.

In general, you'd be better of using fgets().

EDIT: I might be missing the point, but it seems to be that the entire focus on EOF is ... misguided, if all you want to do is read a line. Lines are not generally terminated with EOF, but with \n. So the function should probably just store characters until either EOF or \n is encountered.

unwind
  • 391,730
  • 64
  • 469
  • 606