I / O Methods in C

I am looking for various ways to read / write data from stdin / stdout. Currently I know about scanf / printf, getchar / putchar and gets / puts. Are there any other ways to do this? I am also interested in knowing which one is most effective in terms of memory and space.

Thanks at Advance

+5
source share
3 answers

Stdio is designed to be effective enough no matter which way you prefer to read data. If you need to do step-by-step reads and writes, they are usually expanded to macros that simply access the buffer, unless it is full / empty. For linear io text, use puts/ fputsand fgets. (But NEVER use it gets, because there is no way to control how many bytes it will read!) A family printf(e.g. fprintf) is, of course, extremely useful for text, as it allows you to skip creating a temporary buffer in memory before writing (and thus you can not think about all the problems with memory allocation, overflow, etc.).fscanftends to be much less useful, but mainly because it is difficult to use. If you study the documentation well for fscanfand learn to use %[, %nand numerical specifiers, this can be very powerful!

For large blocks of text (for example, to load the entire file into memory) or binary data, you can also use the freadand functions fwrite. You should always pass 1 for the size argument and the number of bytes to read / write for the argument count; otherwise it would be impossible to find out from the return value how many were successfully read or written.

POSIX ( ), io- open, read, write .. NOT C, POSIX, -POSIX- , , , (, 0,1,2,... POSIX).

+2

, Curses ( * NIX, Windows)

0
source

All Articles