Question:
How do I read input from stdin using system calls?
Nobody
2011-03-31 16:58:40 UTC
My assignment is below, I'm trying to read data from stdin but I honestly have no clue on how to do this according to the assignment spec. Am I supposed to use a standard library function like fgets() or am I supposed to use read(); if I'm supposed to use read, read requires a file pointer does input from stdin even have a file pointer? I'm confused, please clarify what I'm supposed to do and perhaps give me an example on how to do this in C.

PS I looked at section 44.7 and it doesn't give a programming example of how to do this.


The tee command reads its standard input until end-of-file, writing a copy of the input
to standard output and to the file named in its command-line argument. (We show
an example of the use of this command when we discuss FIFOs in Section 44.7.)
Implement tee using I/O system calls. By default, tee overwrites any existing file with
the given name. Implement the –a command-line option (tee –a file), which causes tee
to append text to the end of a file if it already exists. (Refer to Appendix B for a
description
Three answers:
Dan
2011-03-31 17:13:05 UTC
gets() will get from stdin. Maybe not the best, but it should work.
Shadow Wolf
2011-03-31 17:25:18 UTC
C has some permanent file pointers. The usual ones are stdin, stdout, and stderr. You don't need to open or close them like a file. They are always there.



As to what function, what have you covered in class? Some functions use stdin or stdout by default and you don't need to specify a file pointer at all.



I suggest writing part of the assignment and make it work the way it needs to work. Then add the rest of it.



Shadow Wolf
jennis
2016-10-21 13:09:00 UTC
fflush(stdin) isn't standards-compliant C gets() isn't even C in any respect, anymore as for scanf getting "skipped", it is likely a scanf that reads a %c. you may desire to easily prepend that %c with an area, as in scanf(" %c", &var):


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...