Section 3b

Standard I/O in Java


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The Fundamentals of Input and Output

Connecting to a stream. Generally speaking, computer systems tend to treat every component connected to it as a component or a device with objects on or in them that can be opened or closed like data files. This is inherited from the operating system days of the language, and it means that the keyboard, the monitor, the hard drive, your memory sticks and CD or DVD recorder/players are all considered to act like devices managing computer files. The data quantities on or in these devices can be opened or closed, and things can be sent (or written) to them, and/or things can be received (or read) from them.

The metaphor is used that assumes that all the devices are connected to a flowing stream of data inside the computer, called (amazingly enough) a data stream. So if you want to get data from the stream, you must open the device like a file, and either send data to it through the stream, or get data from it through the stream.

For purposes of what are called console operations, meaning operations with console devices called keyboards and monitors, the C language assumes that the standard input and output (I/O) devices, like the keyboard and monitor have already been opened and are prepared for reading/inputting, or writing/outputting. This isn't as purely true as it has been in earlier languages but it is helpful to think of these devices, and the computer interactions, this way.

Doing output. The "file" access point for the console output device (i.e., the computer monitor) is stdout, which is essentially the standard output stream for C language operations, and to get data from the keyboard, you must insert data into the stream using stdin. In reality, these metaphorical "file" access points are really computing system operations that can manage the input and output actions for your programs. The standard operation for output in C is shown here:

printf( "This is an output statement" );

The text data is inserted or sent toward the output device by this operation, and for output, it is common to tell the monitor window to end the line after you have sent some data. The above action simply places text on the screen. One can also add the endline character "\n" to the text to force a carriage return, which ends the line and starts over on the next line.

printf( "This is an output statement\n" );

The print function allows for output of a variety of data quantities in the same string. For example, if you had a variable that held a student's age, a print statement might look like the following.

printf( "You are %d years old", studentAge );

Everywhere there is a percent sign in the text string must have an associated data quantity after the string and the comma. In this case the percent sign with the letter 'd' means print an integer, and studentAge is an integer, found after the end of the text and after the comma.

It is important to consider what your output would look like, and you should check this regularly. The printf operation supports several different kinds of data, including "%s" for string, "%f" for floating point or double, "%c" for character, and so on. You can learn more about using these tools here

Doing input. The C language itself is pretty limited on both input and output operations. Other libraries can be used to manage these (and one is provided in the next section) but the fundamental operation for conducting console input uses the function scanf. The function scanf also uses the same kinds of characters for input (e.g., "%s" for string, "%f" for floating point or double, "%c" for character, etc.). For reasons that go beyond the current scope of your learning at this point, an ampersand ("&") must be placed at the beginning of each value that is captured from the stream. While this is a very effective way of capturing data from the stream, it is not very inuitive or friendly. Use of the scanf operations will be left for later analysis, although you can learn more about them here. For the time being, enjoy using the much friendlier Console_IO_Utilities that you will find in the next section.