C Programming/Input and Output

Objective

 * Learn how to input and output data throught the console.
 * Learn how to input and output data using files.

Introduction
By now you should have noticed the frequent use of the function printf. This is the basic C function for printing text onto a command line terminal.

printf has an unusual (but very powerful) way of displaying text. All information to be displayed is placed between quotation marks. The unusual part is that in order to print the value of a variable, a placeholder must be used within the quotes. To use a placeholder, you write %datatype replacing datatype with a value corresponding to the type of the variable to be displayed. Finally, the name of the variable to be displayed is added after the last quotation mark, but before the semi-colon.

The values for variable placeholders are as follows:

For example, to print the value of an integer variable named "integer_var":

More than one variable can be included in a single printf function. However, for each variable placeholder within the quotation marks, a variable name of matching type must added to the end of the line. For example, to use an integer variable, a float variable, and a single character variable within a printf function, you could type:

Further, printf can also make use of escape characters to represent things that are kept in memory as text but cannot be written directly into your code. One commonly used escape code is \n, which is used to add a line feed to the output. Escape characters always start with a backslash "\".

For example:

will output: the number is 6 (followed by a blank line).

Escape Sequences
Here is a table of most of the escape sequences.

Add in use of stderr, scanf and fgets.

Simple input
For simple input we can use the function scanf. scanf reads formatted input which means it will need the format string to function. A very simple example:

- %d is the format string here, it means a simple integer, a digit.

- x is the variable we will store the integer we've just read, therefore we must pass the memory address of x to scanf. We can do that with the "address" operator &.

- cnt will store number of values we've actually read, if this is less than we intended then obviously something went wrong. So this provides us with some error checking capabilities!

scanf essentially uses the very same format strings as printf, see them on the top of this page.

File I/O
we can open a file in - binary - text modes.

Dealing with text files: Text files can be handled very similar to what we learned about the standard input/output, but now we will use fprintf/fscanf instead of printf/scanf! Their arguments are similar. The only difference is that fprintf/fscanf have 1 more argument. A file pointer! Also we need to open/close the files. It simply means to ask the OS to give us control over that particular file, then after we are done we give it back.

However first of all we need to open the file(s) we want to use.

Opening/using/closing a text file:

1.) First we need to declare a file pointer variable. 2.) Then we need to open it, telling the operating system how we want to open it (text/binary/read/write/append/etc). If the operation is successful we get a file pointer that will tells us where to read from/write. Otherwise we will get a NULL. So it is easy to find out if it was successful or not. 3.) After that we can actually read/write. In this example we write. The return value of fprintf/fscanf is the successfully read/wrote items. So it is easy to find out about errors here too. 4.) finally we need to close the file, it gives the control back to the OS.

'Dealing with binary files: Well binary files are really different than text files. In binary mode, you deal with bytes not characters, and it provides us with a method of storing data more efficiently. What is a matter of utmost importance when dealing with large amount of data (like databases), because it makes your program run faster. It's faster to read/write it from the disk, and takes less disk space! Let's see an example: The number 1985 in a text file, appears as 4 characters: '1','9','8','5'. This takes 4 bytes. However in a binary file it gets stored in the machine's internal format 0x07C1 (2 bytes if we are using short as the variable type). With this little trick we have already saved half the disk space. Opening and closing a file in binary mode is very similar to the method we learned about text mode files. We only have to add 1 more letter to the opening mode string in fopen. However to read and write such files we have brand new and totally different functions.

Opening/using/closing a binary file:

1.) first we need to declare a file pointer variable again, and a buffer variable (this is what we will write to the file). 2.) Now we need to open the file in binary mode. This can be done with adding a 'b' to the opening mode string. 3.) Now we can work with the file. We need to tell fwrite, what we want to write (need to pass a pointer to it, so we point on the data literally), how many times we want to write it, how big the data is we want to write, and where we want to write it. The fread function works almost exactly the same, just the other way around. 4.) Finally we need to close the file

Additional Resources

 * scanf on wikipedia
 * printf on wikipedia

Assignments



 * Previous Lesson: Strings
 * Next Lesson:
 * Course Home Page