Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.objective-c > #211 > unrolled thread

EOF issue

Started bymodelling.data@gmail.com
First post2015-11-18 01:02 -0800
Last post2015-11-19 01:23 -0800
Articles 4 — 4 participants

Back to article view | Back to comp.lang.objective-c


Contents

  EOF issue modelling.data@gmail.com - 2015-11-18 01:02 -0800
    Re: EOF issue Louis Wu <louiswu@ringworld.net> - 2015-11-18 21:02 -0800
    Re: EOF issue "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-19 06:42 +0100
      Re: EOF issue alla.rashitova@gmail.com - 2015-11-19 01:23 -0800

#211 — EOF issue

Frommodelling.data@gmail.com
Date2015-11-18 01:02 -0800
SubjectEOF issue
Message-ID<10c02cc2-fc7c-40c3-8130-9386ec978cbb@googlegroups.com>
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. But how does it work when referred
to a function within a program. 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).

/** 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 main(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 0;
}

Thank you!

[toc] | [next] | [standalone]


#212

FromLouis Wu <louiswu@ringworld.net>
Date2015-11-18 21:02 -0800
Message-ID<louiswu-85C343.21025818112015@news.giganews.com>
In reply to#211
In article <10c02cc2-fc7c-40c3-8130-9386ec978cbb@googlegroups.com>,
 modelling.data@gmail.com wrote:

> 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. But how does it work when referred
> to a function within a program. 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).
> 
> /** 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 main(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 0;
> }
> 
> Thank you!

Do you think, maybe, that it's reading characters that you type them in 
from the keyboard ...

[toc] | [prev] | [next] | [standalone]


#213

From"Pascal J. Bourguignon" <pjb@informatimago.com>
Date2015-11-19 06:42 +0100
Message-ID<87mvuay281.fsf@kuiper.lan.informatimago.com>
In reply to#211
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

[toc] | [prev] | [next] | [standalone]


#214

Fromalla.rashitova@gmail.com
Date2015-11-19 01:23 -0800
Message-ID<bf330c1d-8c66-41d5-be88-50010e9968f7@googlegroups.com>
In reply to#213
<snip>
> 
> 
> 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
> 
Thank you very much for your help and for suggesting the book, and the 
blog. I have ordered the book. 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.objective-c


csiph-web