How can I fix my copy program to properly handle the EOF value and stop running?

  • Thread starter Thread starter Ja4Coltrane
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a copy program in C that fails to terminate properly when reaching the end of input, specifically regarding the handling of the EOF (End Of File) value. Participants explore various aspects of input handling in different operating systems and provide suggestions for code improvement.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant describes their issue with a copy program that does not stop running even when EOF is entered directly.
  • Another participant notes that in Windows, EOF is represented by Control Z, suggesting this as a potential solution.
  • A participant using PuTTY and compiling with GCC over SSH mentions that Control Z suspends the program instead of signaling EOF, leading to confusion about program termination.
  • It is pointed out that on Unix systems, EOF is indicated by Control D, and Control Z is used to background the process, which is not the desired behavior for input handling.
  • One participant expresses curiosity about why directly assigning -1 to the variable does not end the program, asking for clarification on what Control D does in this context.
  • A response explains that EOF is a signal from the operating system indicating the end of input, and that getchar() returns an int to accommodate this signal. It also advises against assuming EOF is always -1, emphasizing the importance of comparing against the EOF macro.
  • Another participant suggests a more common coding pattern for the while loop to avoid potential bugs related to input handling.

Areas of Agreement / Disagreement

Participants generally agree on the proper way to signal EOF in different operating systems, but there is no consensus on the implications of directly assigning -1 to the variable or the best coding practices to avoid bugs.

Contextual Notes

There are limitations in understanding how EOF is defined across different systems, and the discussion highlights the importance of recognizing the behavior of input functions in various environments.

Who May Find This Useful

This discussion may be useful for programmers, particularly those learning C or dealing with input handling in different operating systems, as well as students working on computer science projects related to file and input management.

Ja4Coltrane
Messages
224
Reaction score
0
Hello.

So I am trying to write a simple copy program. For whatever reason, the program will never stop running! Even if I directly enter the value of EOF (-1 in my case), it still goes.


Code:
#include <stdio.h>

main()
{
	int c;
	c = getchar();

	while ( c != EOF)  {
		putchar(c);
		c = getchar();
	}
}
 
Technology news on Phys.org
If you're running that in a dos box under Windoze, then EOF is defined as Control Z on standard in.

Try that.
 
I'm using putty and compiling with gcc over ssh. I tried control z and it outputed "suspended" and ended the program, however that was not what I wanted as it did not continue the program after the while loop (if say, I added printf("done\n"); after the loop). Any suggestions?
 
On unix, EOF on a console is ctrl D
Ctrl Z puts the program into the background, which isn't what you want if it is taking input from stdin.
To get the program back type 'fg' then kill it with ctrl C
 
Wow that worked! Thank you very much mgb_phys! Would someone mind explaining in more detail why that works--I mean, if EOF is simply the integer -1, then why is it that letting c = -1 does not end the file. What does control D actually input into the program?
 
Thats what the operating system is for.
It's job is to read a file off disk and present it to the program a byte at a time, or in your case, read the keyboard convert it into ascii characters and send them to your program You obviously need a way of signalling that you have finished entering your text. One way would be to have a special key 'end' on the keyboard but Unix was designed to work on many different machines with different keyboards so uses ctrl+letter for all these extra characters.

This is also the reason getchar() returns an int not a char. If it only handled chars there wouldn't be any room for the extra commands like EOF.

ps Don't rely on EOF being -1, it can be anything the library and OS choose, always compare it to EOF.

pps. I didn't want to confuse things before, but the more common way to write the code would be:
while ( (c=getchar()) != EOF ) {
putchar(c);
}
Then you don't need the initial getchar() outside the loop. This is a common source of bugs, if it gets removed or other code gets called between it and the test.
 
Last edited:
Or
Code:
while ( (c=getchar()) != EOF )  {
	putchar(c);
}
 
Thanks KTC, I fixed the typo.
 
Interesting. Thank you very much for the help.
 
  • #10
Stick at Ja4Coltrane. Starting at this level is important if you ever want to really understand computers whatever language you use.
Just hooking the Java input thing to the Java output thing and not knowing what is actually happening only gets you so far.
 
  • #11
genious.man...mgb_phys
 
  • #12
Ja4Coltrane, your original post helped me finish one of my comp sci. projects!

thanks for your post!
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
6K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
14
Views
4K
Replies
4
Views
5K
Replies
81
Views
7K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K