This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
EndOfFile in C - EOF
I was trying examples from K&R. I am not able to understand why this code does not exit unless ctrl+c is pressed.
int main ( )
{
int c;
c = getchar();
while(c!=EOF)
{
putchar(c);
c=getchar();
}
}
Any help is appreciated. Thanks!
EDIT: Using Windows (Visual Studio 2010)
In Windows, you generate end of file from the standard input stream by pressing Ctrl+Z. Depending on the buffering behavior, you might also need to press Return.
EOF is End of File. If you read from 'keyboard', you should compare to End of Line symbol which is equal to press Return
int main ( )
{
int c;
c = getchar();
while(c!= '\n')
{
putchar(c);
c=getchar();
}
}
On Windows machine ctrl+c acts as delimiter of character scanning same as EOF
on this loop gets broken otherwise it will keep looking for characters
Related
I am currently reading K&R's book and typing in the examples from the first section, and there are a couple of examples such as this:
while((c = getchar()) != EOF) {
//do something
}
I am testing these examples on a Windows box and thus running the compiled exe files from the cmd prompt.
To test the example above, how do I simulate an EOF? That is, basically how can I make the loop stop when testing the example from the command prompt?
To enter an EOF, use:
^Z (CtrlZ) in Windows
^D on Unix-like systems
Refer EOF
Windows: Ctrl+Z
Unix :Ctrl+D
First, press: Ctrl^X, next: Ctrl^D
You can also simulate EOF by explicitly giving int variable a value of -1.
Check out this code for more clarity:
#include<stdio.h>
int main() {
// char ch=getchar()
// int ch=-1;
if(ch==EOF) { printf("\nEOF: %d",EOF); }
if((ch!=EOF)==0) { printf("\nit is equal to 0"); }
if((ch!=EOF)==1) { printf("\nit is equal to 1"); }
else { printf("\n it is equal to other value"); }
system("pause");
return 0;
}
I had the same problem after pressing Ctrl+d program stopped and returned 0.
If you use Clion press Ctrl+Shift+a than type Registry press enter and make sure run.processes.with.pty. is unchecked.
After that compile program again and then you can type in input but don't press Ctrl+d on the same line as input it will return 0 or Error.
This question already has an answer here:
what is EOF in cygwin in windows 10
(1 answer)
Closed 4 years ago.
I recently installed Cygwin64 and I am trying to run a C program which takes input from the keyboard using getchar
#include <stdio.h>
// copies input to output
int main()
{
int c;
while ((c = getchar()) != EOF)
putchar(c);
}
I have used stty -a to check what keyboard signals do what and EOF is correctly mapped to ctrl+D however it does nothing
I have read that there may be a conflict with MinGW which I am also using
My solution is switching to using the Cygwin terminal inside VS Code which instead uses windows keyboard signals
It's still Ctrl+D. You have a typo in your code. You are not assigning the return value of getchar to c. Remove an equals sign.
while ((c = getchar()) != EOF)
This question already has answers here:
How to send EOF via Windows terminal
(3 answers)
Closed 5 years ago.
I am working through K&R's exercises right now and I am at the one where you count the number of blanks, spaces, and tabs using the C language. I have built the following code:
#include <stdio.h>
#include <stdlib.h>
/*Write a program that counts blanks, tabs, and newlines*/
int main()
{
int c, numblanks, numtabs, numnewlines;
numblanks = 0;
numtabs = 0;
numnewlines = 0;
printf("Enter some text and press \"Enter\"\n");
while ((c = getchar()) != EOF) {
if (c == ' ')
++numblanks;
if (c == '\t')
++numtabs;
if (c == '\n')
++numnewlines;
}
printf("The total number of blanks is %i\n", numblanks);
printf("The total number of tabs is %i\n", numtabs);
printf("The total number of new lines is %i\n", numnewlines);
}
I am using Codeblocks and the built-in GCC compiler that is installed with it on a Windows 10 OS. When I run the program, I type some text into the program window that pops up and press "Enter" and nothing happens. I am not sure why. I was wondering if someone could help me get another pair of eyes on my code to see if there is something I am missing. Here is an image of what happens in the program when I run it:
Program window with typed text, but no reaction
I have not tried your solution but sometimes this can happen when the program is finished, the terminal closes. Try to put a getchar at the end of your program.
printf("Enter key to exit");
getchar();
This can help after string input (but sometimes require creating loop with getchar() and checking the result of this function)
This question already has answers here:
End of File (EOF) in C
(3 answers)
Closed 7 years ago.
When I use this code, I can type into the command line and get back what I typed:
main() {
int c;
while ((c = getchar()) != EOF) {
putchar(c);
}
}
Output:
~/code/c $ ./a.out
one
one
two
two
But when I use this code, it only works when I pipe in data, not when I type data into the command line:
main() {
int nc;
nc = 0;
while (getchar() != EOF) {
nc++;
}
printf("%d\n", nc);
}
When I pipe data in from a text file:
~/code/c $ ./a.out < practice.txt
14
When I try to input data from the command line:
~/code/c $ ./a.out
one
two
three
What's going on?
The while loop never exits since while (getchar() != EOF) is always true. After you're done with the input, press Cntrl+D for Linux or Ctrl+Z for Windows to indicate EOF.
Like everyone mention, you program doesn't stop. The while loop needs a condition to stop. you have to either use CTRL+Z (Win) or CTRL+D(Unix) to stop it, When you pipe it actually sends that character for you. Because EOF is END_OF_FILE. So when you hit the end of your file. the OS sends that character for you.
So I started to learn C using the ANSI C book. One of the early exercises in the book is to write a program that takes text input and prints every word on a new line, simple enough. So i did:
#include <stdio.h>
#define IN 1
#define OUT 0
main() {
int c;
int state;
state = OUT;
while((c = getchar()) != EOF){
if(c != ' ' && c != '\n' && c != '\t'){
state = IN;
}else if(state == IN){
state = OUT;
putchar('\n');
}
if(state == IN)
putchar(c);
}
getchar();
}
The thing is that while the program works fine it won't break from the while loop if I enter EOF(Ctrl+Z on windows) as the last char of a line or in the middle of it.
So I found an answer here.
What I learned is that the (Ctrl+Z) char is some sort of signal to end the stream and it must be on a new line for getchar() to return EOF. While this is all good and it kinda helped I really want to know why is it necessary for the EOF to be on its own line?
The problem you are having is related to your command line terminal and has nothing to do with the end of file marker itself. Instead of sending characters to the program as you type them, most terminals will wait until you finish a whole line before sending what you type to the program.
You can test this by having the input come from a text file instead of being typed by hand. You should be able to end the input file without a newline without any problems.
./myprogram.exe < input.txt
By the way, the answer you linked to also points out that EOF is not a character that is actually in your input stream, so there is no way for it to come "before" a "\n". EOF is just the value that getchar returns once there are no characters left to be read.
When reading from a tty device (such as stdin for a program running in a console or terminal window) the terminal is in so-called cooked mode. In this mode, some level of line editing facilities are provided, allowing the user to backspace and change what has been typed.
The characters that are typed are not returned to the program until after return has been pressed.
It is possible to do this by placing the terminal in 'raw' mode. Unfortunately it seems this is not well standardised though, so it is somewhat system specific. The answers to this question have some examples for various platforms.