What is stdin in C language?

蓝咒 提交于 2021-02-20 19:08:41

问题


I want to build my own scanf function. Basic idea is data from a memory address and save it to another memory address.

What is stdin? Is it a memory-address like 000ffaa? If it is a memory-address what is it so I can build my own scanf function. Thanks!.


回答1:


No, stdin is not "a memory address".

It's an I/O stream, basically an operating-system level abstraction that allows data to be read (or written, in the case of stdout).

You need to use the proper stream-oriented I/O functions to read from the stream.

Of course you can read from RAM too, so it's best to write your own function to require a function that reads a character, then you can adapt that function to either read from RAM or from stdin.

Something like:

int my_scanf(int (*getchar_callback)(void *state), void *state, const char *fmt, ...);

Is usually reasonable. The state pointer is some user-defined state that is required by the getchar_callback() function, and passed to it by my_scanf().




回答2:


stdin is an "input stream", which is an abstract term for something that takes input from the user or from a file. It is an abstraction layer sitting on top of the actual file handling and I/O. The purpose of streams is mainly to make your code portable between different systems.

Reading/writing to memory is much more low-level and has nothing to do with streams as such. In order to use a stream in a meaningful way, you would have to know how a certain compiler implements the stream internally, which may not be public information. In some cases, like in Windows, streams are defined by the OS itself and can get accessed through API calls.

If you are looking to build your own scanf function, you would have to look into specific API functions for a specific OS, then build your own abstraction layer on top of those.




回答3:


On Unix everything is a file

https://en.wikipedia.org/wiki/Everything_is_a_file

Or like they notice

Everything is a file descriptor

You can find on unix system /dev/stdin who is a symbolic link to /dev/fd/0 who is a Character special file



来源:https://stackoverflow.com/questions/37719915/what-is-stdin-in-c-language

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!