EOF reading C/C++ - c

I'm using NetBeans MinGW to compile simple c programs(I'm new at this).
My problem is that I have this simple code
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
int c,i=0;
while((c=getchar())!=EOF){
i++;
}
printf("%d",i);
return 0;
}
and when I try to end input like this:
hello^Z [enter]
it doesn't do it, I need to re-enter
^Z[enter]
to end it.
I'd appreciate that you tell me why this happens.
Thanks in advance

C input is line-oriented by default. Ending a line with the EOF character (^Z on Windows, ^D on Unix) ends the line (without a trailing newline) but doesn't actually signal end of file; the end of file condition is signaled when it's encountered on the next read, i.e. at the beginning of a line.

Just the way the console handles input
Ctrl-Z on a UNIX system would be an interrupt to let you suspend the process, so I guess it is a Windows console.
When you Ctrl-Z after the characters it probably does treat this as an "End" which is Ctrl-Z on its own.

Related

Strange behavior in terminal raw mode

I'm following a tutorial for making a text editor .
So far it's been tinkering with raw mode . The following code is supposed to turn off canonical mode , and output each keypress.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
struct termios orig_termios;
void disableRawMode() { … }
void enableRawMode() { … }
int main() {
enableRawMode();
char c;
while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q') {
if (iscntrl(c)) {
printf("%d\n", c);
} else {
printf("%d ('%c')\n", c, c);
}
}
return 0;
}
I originally forgot to add "\n" after the printf() statements, and the result was that I only got the outputted characters after the program terminates , ie after pressing q in this example .
However after adding "\n", the terminal outputs each letter as pressed.
Could anyone be so kind and explain why is it behaving this way?
Raw-mode is the concern of the terminal but buffer management of stdout occurs before reaching the terminal.
By default, when file-descriptor 1 (STDOUT_FILENO) is link to a terminal, then stdout uses a line-buffering policy.
This means that the output buffer of stdout is flushed to the file-descriptor 1 when a \n is written (or when it is full).
Only at this moment the characters can reach the terminal which can react in different ways depending on its configuration.
In your example, the characters just stay in memory until the process terminates (stdout is flushed at this moment).
Commonly, when a C program starts with the standard output stream connected to a terminal, the stream is line buffered. This means characters printed with printf or standard library methods are kept in a buffer until \n is printed (ending the line, hence “line buffered”), the buffer is full, when the stream is manually flushed (as with fflush), or when input is solicited on stream that is unbuffered or that is line buffered but requires characters from “the host environment” (notably a human).
The terminal setting is irrelevant as the characters are kept in an internal buffer of the standard C library implementation and are not sent to the terminal until one of the above events.
You can set the stream to unbuffered by calling setvbuf(stdout, NULL, _IONBF, 0) before performing any other operation on stdout.

Read loop wont terminate

I can't seem to get this read loop to terminate.
#include <unistd.h>
char buffer[256];
int read_chars;
while((read_chars = read(STDIN_FILENO,buffer,sizeof(buffer))) > 0) {
//DO STUFF
}
However, when I hit enter in the terminal, it just hangs for the next read and never exits.
Any thoughts on this implementation is not correct ?
Here read will stop reading at the end of standard input, which a newline isn’t. Try doing Ctrl-D twice in your terminal while the program is reading and it should terminate. Ctrl-D once on an empty line or twice after some characters marks the end of standard input.

K&R C Counting Characters

I'm working out of the K&R book, and I'm on the code example of how to count characters from a stream of text. I copied their code and tried running it, but when the command line prompts you for characters, the loop doesn't exit and thus will never print out the character count. Is there an error here I'm not catching?
#include <stdio.h>
main()
{
long nc;
nc = 0;
while(getchar() != EOF) {
++nc;
}
printf("%1d\n", nc);
}
Whenever you want to stop it just send the EOF signal to the shell.
Ctrl+d in Linux or Ctrl+z on Windows.
By the way (as additional info) Ctrl+c send SIGINT to a process in Linux and on Windows it does something similar.
Have you tried to press Ctrl+D (on Linux) or Ctrl+Z (on Windows)? If yes then It will come out of loop for sure. On pressing these keys, it will return EOF and loop will terminate.
Try ending your character stream with CNTL-Z (end of file character). Just hitting Enter results in a CR which is just another character to count

unusual behaviour with printf with infinite while loop

When I run the below program, I do not get any output.
#include <stdio.h>
int main()
{
printf("hello");
while(1)
{
}
return 0;
}
whereas if i edit the printf command to add a '\n' character to the end of the string, then the expected output comes. what is going on in the first code? I simply cannot understand it.
This is because stdout is line buffered, i.e. the output is not written to the device (the terminal) until a full line has been collected.
You can call fflush(stdout); to force a flush of the buffer to the terminal. Do not try to flushing stdin by the way, that's not allowed.
try
printf("hello\n");
or
printf("hello");
fflush(stdout)
Use printf("hello\n");
For more info see answers to this question.

What does "end of file" mean in C?

#include <stdio.h>
main()
{
int c;
while ((c = getchar()) != EOF)
putchar(c);
}
In the above code, which character will break the loop?
I am new to C, please help me.
Also, what is it meant by this error:
codec5.c:8:2: warning: no newline at end of file
The warning just means that you need to have a new line at the end of your source code. Put your cursor at the last } in your file at the end of main() and press enter.
You need to check for a specific character to end the program if you are not loading from a file. If you pipe (|) (< in Windows) a file into your program, then the program should end. If your program is named test.exe and your input file is foo.txt, try running test.exe < foo.txt (make sure they are in the same directory).
The error is solved by putting a newline at the end of the file (put the cursor behind the } and press enter).
I think the loop is broken with Ctrl+Z, but I'm not sure about that.
It is a special constant defined in stdio.h which means the end of the file.
codec5.c:8:2: warning: no newline at end of file
Sounds like you don't have a \n at the end of your file :)

Resources