简体   繁体   中英

How can I read from stdin with this specific command?

I'm very new to C and I like to know what my program needs so it is able to read from a given test.stdin file, when this command is written to the console:

cat test.stdin | ./myprogram | tee myoutput.stdout

?

So I guess the test.stdin is some file on the computer which feeds my program with data/input, then my program is doing something with that data and writes the result into the file myoutput.stdout which is also somewhere on my computer.

What do I need in my program so the command above is working? I need to do some calculations with the data in stdin, but I don't know how to begin, how to read like that and make the command work for my program.

In C, reading from "standard input", or stdin , is the default. It's the easiest thing there is. You don't have to explicitly open anything; stdin is always opened for you (along with stdout and stderr ) before your main function even gets called.

You can read from stdin with a variety of standard library functions. The most common are getchar and scanf . And, to repeat: you can just start calling them, on the first line of main , if you like. They work right away; there are no setup steps required.

getchar reads one character.
scanf reads "formatted input", although this function is full of problems and gotchas, to the point that it's almost not worth using.

To read one line from stdin , the function used to be gets , although it had a fatal problem and has been removed from the language. Today, to read one line from stdin , one way to do it is with the fgets function, which can actually read lines from any stream, so you have to explicitly pass it stdin as its fp argument.

stdout (Standard output) is a place to which a program can send information (eg, text). The program never knows where the information it sends to standard output is going. The information can go to a printer, an ordinary file, or the terminal(screen). By default the shell directs standard output from a command to terminal(screen) and describes how you can cause the shell to redirect this output to another file.

stdin (Standard input) is a place a program gets information from; by default, the shell directs standard input from the keyboard. As with standard output, the program never “knows” where the information comes from. The following sections explain how to redirect standard input to a command so it comes from an ordinary file instead of from the keyboard.

You can cause the shell to redirect standard input or standard output of any command by associating the input or output with a command or file other than the device file representing the keyboard or the terminal(screen).

The cat utility provides a good example of the way the keyboard and terminal(screen) function as standard input and standard output, respectively. When you run cat, it copies a file to standard output. Because the shell directs standard output to the screen, cat displays the file on the screen.

cat writes to the standard output, which is not necessarily a terminal, even if cat was typed as part of a command to an interactive shell. If you really need something to write to the terminal even when the standard output is redirected, that is not so easy (you need to specify which terminal, and there might not even be one if the command is executed from a script)

You can just read from stdin , as @MikeCAT says, but as a beginner you may not know how to do that. You can use getchar , fgets , and scanf , to name a few. There is no need to "open" stdin , because it is opened by default for any C program, as are stdout and stderr . If you are running on Linux, you can learn about the usage of the various input functions by using man ... ie, man getchar . If you're not on Linux, you can just do a google search .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM