C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-05-2009, 09:47 PM   #1
Registered User
 
Sharke's Avatar
 
Join Date: Jun 2008
Location: NYC
Posts: 262
Question about 'gets' and EOF

I've never been sure about how EOF works on a system. Take for instance this example program, which counts strings entered until it encounters the word "quit" or the EOF.

Code:
#include <stdio.h>
#include <string.h>
#define SIZE 81
#define LIM 100
#define STOP "quit"

int main(void)
{
  char input[LIM][SIZE];
  int ct = 0;

  printf("Enter up to %d lines (type quit to quit):\n", LIM);

  while (ct < LIM && gets(input[ct]) != NULL && strcmp(input[ct], STOP) != 0)
    ct++;

  printf("%d strings entered\n", ct);
  return 0;
}
Then the following expected behavior is given:

Quote:
1
2
3
4
5
^Z
5 strings entered
But what is happening here?
Quote:
1
2
3
4
5^Z
^Z
^Z
5 strings entered
And here?
Quote:
1
2
3
4
5^Z
6
7
8
9
^Z
8 strings entered
Sharke is offline   Reply With Quote
Old 03-05-2009, 10:30 PM   #2
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,203
Is this another one of those trick questions at the end of Apocalypse Now where Brando snarls, "I don't seen any EOF at all"?

Because if it is it ain't funny, really.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Old 03-05-2009, 11:40 PM   #3
MENTAL DETECTOR
 
whiteflags's Avatar
 
Join Date: Apr 2006
Location: United States
Posts: 3,295
The gets function, like fgets, is usually written as a looped call to fread. Your question is answered in the manual:

Quote:
The functions fread() and fwrite() advance the file position indicator
for the stream by the number of bytes read or written. They return the
number of objects read or written. If an error occurs, or the end-of-
file is reached, the return value is a short object count (or zero).
So if you send EOF immediately after some data, it is likely that fread will return "a short object count" instead of zero like you expected, which ultimately determines gets return value. It is no different from what happens when reading a file. You may encounter EOF while you read the last line, but only the call after that fails for certain. This is so the last of the data has an opportunity to be processed.

And remember that when you send EOF from the console there is actually two pieces of data to read: the EOF, and a newline. If the newline weren't there, input would fail like you expect.
__________________
<Niggawatts> Writing is both mechanical and organic
<Niggawatts> It's like a cyborg dragon.
<Niggawatts> Writing is like a cyborg dragon.

Last edited by whiteflags; 03-05-2009 at 11:45 PM.
whiteflags is offline   Reply With Quote
Old 03-11-2009, 08:39 AM   #4
vyn
Registered User
 
Join Date: Nov 2008
Posts: 7
Quote:
1
2
3
4
5^Z
6
7
8
9
^Z
8 strings entered
I have the same question: how do I prevent the above from happening? I want the input to terminate on pressing Ctrl-D/Z. The behaviour should be like the following:

Quote:
1
2
3
4
5^Z
5 strings entered
The Unix cat program seems to be able to do it. How do I do the same thing in C?
vyn is offline   Reply With Quote
Old 03-11-2009, 08:41 AM   #5
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,353
vyn, start a new thread and post your current code.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 07:07 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22