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

  • Thread starter Ja4Coltrane
  • Start date
In summary, the program will never stop running even if you enter the value of EOF (-1 in this case). To get the program back type 'fg' then kill it with ctrl C.
  • #1
Ja4Coltrane
225
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
  • #2
If you're running that in a dos box under Windoze, then EOF is defined as Control Z on standard in.

Try that.
 
  • #3
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?
 
  • #4
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
 
  • #5
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?
 
  • #6
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:
  • #7
Or
Code:
while ( (c=getchar()) != EOF )  {
	putchar(c);
}
 
  • #8
Thanks KTC, I fixed the typo.
 
  • #9
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!
 

What is the purpose of C putchar() and getchar()?

C putchar() and getchar() are two functions in the C programming language that allow for input and output of single characters from the console. They are used primarily for user interaction and reading/writing characters to and from files.

How do I use C putchar() and getchar() in my code?

To use C putchar() and getchar(), you must first include the stdio.h header file in your code. Then, you can use the functions by calling putchar() to display a character and getchar() to read a character from the console or a file.

Can I use C putchar() and getchar() with strings or other data types?

No, C putchar() and getchar() can only be used with single characters. If you want to work with strings or other data types, you will need to use other functions or methods.

What happens if I try to use C putchar() and getchar() without including the stdio.h header file?

If you try to use C putchar() and getchar() without including the stdio.h header file, your code will not compile. This is because the functions are defined in the stdio.h library and the compiler needs to know their definitions in order to execute them properly.

Are C putchar() and getchar() standard functions in all C compilers?

Yes, C putchar() and getchar() are standard functions in the C programming language and are included in all C compilers. However, some compilers may have additional functions or extensions that are not part of the standard library.

Similar threads

  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
15
Views
5K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
4
Views
734
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top