count the number of lines, words, and characters within an input

前端 未结 6 1909
礼貌的吻别
礼貌的吻别 2021-01-19 06:20

Right now I am going through a book on C and have come across an example in the book which I cannot get to work.

#include 
#define IN 1
#defin         


        
相关标签:
6条回答
  • 2021-01-19 06:23

    I usually handle this kind of input like this (for Linux): 1. make a file (for example, named "input.txt"), type your input and save 2. use a pipe to send the text to your application (here assume your application named "a.out" and in the current directory):

    cat input.txt | ./a.out
    

    you'll see the program running correctly.

    0 讨论(0)
  • 2021-01-19 06:36

    It's waiting for input on stdin. Either redirect a file into it (myprog < test.txt) or type out the data and hit Ctrl-D (*nix) or Ctrl-Z (Windows).

    0 讨论(0)
  • 2021-01-19 06:36

    When you run it, you need to type in your text, press return, then type Ctrl-d and return (nothing else on the line) to signify end-of-file. Seems to work fine with my simple test.

    0 讨论(0)
  • 2021-01-19 06:46

    The program only terminates when the input ends (getchar returns EOF). When running on terminal, this normally never happens and because of this it seems that the program is stuck. You need to close the input manually by pressing Ctrl+D (possibly twice) on Linux or pressing F6 and Enter at the beginning of the line on Windows (different systems may use different means for this).

    0 讨论(0)
  • 2021-01-19 06:47

    What it is doing is entering a loop for input. If you enter a character or newline, nothing happens on the screen. You need to interrupt the process (on my Mac this is CTRL+D) which serves as EOF. Then, you will get the result.

    0 讨论(0)
  • 2021-01-19 06:47

    getchar() returns the input from the standard input. Start typing the text for which you want to have the word count and line count. Your input terminates when EOF is reached, which you do by hitting CTRL D.

    CTRL D in this case acts as an End Of Transmission character.

    cheers

    0 讨论(0)
提交回复
热议问题