From the course: C Standard Library

Unformatted I/O - C Tutorial

From the course: C Standard Library

Start my 1-month free trial

Unformatted I/O

- [Instructor] Let's see some functions for unformatted input output. These are basic functions for reading and writing data. That is, to retrieve data from some source, or to send data to some target. These functions are intended for files stored in the computer and character streams, like the console keyboard input, or the text output on the screen. The term unformatted means that the data is not interpreted to be encoded for printing. For example, a formatting output function could take the value 12 and send out the text characters one and two as a string. This is not the case for unformatted IO. The functions we'll see are old members of the putc, getc, puts, and gets family. There are four functions I'd like to cover. First, we have getchar, which reads a one byte character from the character input stream, and returns that character promoted to the int type on success, or eof on failure. This function is equivalent to calling the more primitive function getc, passing stdin as the argument. Next, we have putchar, which writes a one byte character to the character output stream. This character is sent as an int type parameter, which is converted to unsigned char. Calling this function is equivalent to calling putc, passing the character and stdout as arguments. This function returns the sent character as an int type on success or eof on failure. The fgets function, takes three arguments: A string, str, an integer count, and a file stream. It reads at most count characters from the stream until a new line or eof is found. It then writes these characters into the string and it returns the string on success. It returns null on failure. Now, this is a replacement for an earlier gets function, which prove dangerous because of not checking bounds, thus being vulnerable to buffer overflow attacks. In C11, the gets function was removed and it's usage is discouraged. And finally, the puts function, writes to stdout, a string pointed to by it's str argument. It returns a non negative number on success, or eof on failure.

Contents