modelling.data@gmail.com writes:
Hello!
EOF means that the input has reached the end of the file. I think
I understand how it might work when I use some external file (for
example, txt one) in the program.
I doubt it.
For example, few people seem to understand that end-of-file is a
transient condition: files may grow between the time you reach the end
of the file, and the time you try it.
But how does it work when referred to a function within a
program.
What do you mean by that?
Below is a program that counts words, lines and characters,
but the loop never seem to terminate when I run the program (program
for K&R).
[…]
while ((c = getchar()) != EOF)
[…]
As indicated by Louis, getchar() will read stdin which by default is
bound to the terminal. How can there be an end of file of the terminal?
Do you unhook that terminal? What about when it's a virtual terminal
emulated in a window by a terminal emulator program? Do you kill that
program?
Type:
man 1 stty
in a terminal.
The terminal will signal the transient condition of an end-of-file when
you type the eof character configured with stty for that.
By default, it's often Control-D.
Type:
stty -a
in the terminal to see what's the current configuration.
Notice the other useful conditions that can be indicated with such
characters. For example, sending an INTR signal (often configured as Control-C).
Now of course, the funny thing is that after having indicated the
end-of-file on the terminal, nothing prevents further reading from the
terminal (and it better be!). So for example, in the following program
you can signal several time the end-of-file condition on the terminal:
[pjb@kuiper localhost:10.0 spl]$ cat wc.c
/** Program to count lines, words, characters in a word **/
#include <stdio.h>
#define IN 1 //inside a word
#define OUT 0 //outside a word
int wc(void){
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF){
nc++;
if (c == '\n'){
nl++;
}if (c ==' ' || c == '\n' || c == '\t'){
state = OUT;
}else if (state == OUT){
state = IN;
nw++;
}
}
printf("%d %d %d\n", nl, nw, nc);
return nl;
}
int main(void)
{
while(0!=wc()){
// nothing
}
return 0;
}
[pjb@kuiper localhost:10.0 spl]$ cc -g3 -O0 -std=c99 -std=c11 wc.c -o wc
[pjb@kuiper localhost:10.0 spl]$ ./wc
hello
world
^D
2 2 12
hello
^D
1 1 6
^D
0 0 0
[pjb@kuiper localhost:10.0 spl]$
(^D indicates when I typed Control-D to signal the end-of-file condition. Notice, inside an emacs shell buffer, you'd have to type C-c C-d.)
With unix disk files, it wouldn't be possible to signal several times
the end-of-file condition (unless the file grows between each test).
But on tapes, the end-of-file is generally indicated by a specific
signal written on the tape. After that signal, another file can be
recorded. When the "end-of-tape" is reached, two consecutive
end-of-file signals are written on the tape. Therefore you could use
this version of wc to get the line count in all the files of a tape,
with something like:
./wc < /dev/rmt0
Note: ALWAYS use brackets for if, while, do, for, etc in C.
if(test1){
one();
}else if(test2){
unique();
}else{
single();
}
If you don't, you might obtain things like:
if(test)
goto somewhere;
goto somewhere;
and cost billions of dollars in cracking to everybody in the world, and
have thousands of people killed by oppressive political regimes.
cf.
https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/
Didn't I mention earlier:
http://www.amazon.fr/C-Traps-Pitfalls-Andrew-Koenig/dp/0201179288/ref=sr_1_1?ie=UTF8&qid=1447910932&sr=8-1&keywords=pitfalls+c
You should not attempt programming in C without having read this, and
keep reading it until you know it by heart while learning C. And this
is a minimum, there are much more problems with C. Eg. another good
reading would be:
http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html
--
__Pascal Bourguignon__
http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)