Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

How to Use Stream Input in C Programming

The basic input/output functions in C are not interactive, which means that they don’t sit and wait for you to type text at the keyboard. That’s the way you expect to use a computer program: You type input, and the program reacts directly. But standard input in C isn’t character based, it’s stream based.

With stream-based input, a program looks at the input as though it were poured out of a jug. All the characters, including Enter, march in one after another. Only after a given chunk of text is received, or input stops altogether, does the stream end. This concept can be frustrating to any beginning C programmer.

Basics of stream input

Consider the code illustrated in Foiled by Stream Input. It appears that the code reads input until the period is encountered. At that point, you would assume that input would stop, but that’s not anticipating stream input.

FOILED BY STREAM INPUT

#include <stdio.h>
int main()
{
 char i;
 do
 {
 i = getchar();
 putchar(i);
 } while(i != '.');
 putchar('\n');
 return(0);
}

Exercise 1: Type the source code from Foiled by Stream Input into an editor. Build and run to try out the program. Type a lot of text and a period to see what happens.

Here’s how it runs:

This is a test. It's only a test.
This is a test.

Generally speaking, the program doesn’t halt input after you type a period. The first line in the preceding example is the stream, like a fire hose shooting characters into the program. The program behaves properly, processing the stream and halting its display after the period is encountered. The Enter key serves as a break in the stream, which the program uses to digest input until that point.

How to deal with stream input

Despite the C language’s stream orientation, ways do exist to create more-or-less interactive programs. You simply have to embrace stream input and deal with it accordingly.

The source code in Fishing for Characters in the Stream should be pretty straightforward to you. The getchar() function fetches two characters and then the characters are displayed on Line 11.

FISHING FOR CHARACTERS IN THE STREAM

#include <stdio.h>
int main()
{
 char first,second;
 printf("Type your first initial: ");
 first = getchar();
 printf("Type your second initial: ");
 second = getchar();
 printf("Your initials are '%c' and '%c'\n", first,second);
 return(0);
}

Exercise 2: Type the source code from Fishing for Characters in the Stream into your editor. Line 11 is split so that it doesn’t wrap; you don’t have to split the line in your editor. Build and run using your initials as input.

Here’s the output:

Type your first initial: D
Type your second initial: Your initials are 'D' and '
'

It never gave a chance to enter your second initial. The stream included the Enter key press, which the program accepted as input for the second getchar() function. That character, \n, is displayed in the output between the single quotes.

How do you run the program? Simple: Type both initials at the first prompt:

Type your first initial: DG
Type your second initial: Your initials are 'D' and 'G'

Of course, that’s not what the code asks for. So how do you fix it? Can you think of a solution using your current programmers’ bag o’ tricks?

Don’t give up!

The solution is to devise a function that returns the first character in the stream and then swallows the rest of the characters until the \n is encountered. That function looks like A Single-Character Input Function, Getch():

A SINGLE-CHARACTER INPUT FUNCTION, GETCH()

char getch(void)
{
 char ch;
 ch = getchar();
 while(getchar()!='\n')
 ;
 return(ch);
}

To wrap your brain around stream input, consider that the while loop in A Single-Character Input Function, Getch() spins through all text in the stream until a newline is encountered. Then the first character in the stream, grabbed at Line 5, is returned from the function.

Exercise 3: Modify the source code from Exercise 2 so that the getch() function illustrated in A Single-Character Input Function, Getch() is used to gather input. Build and run to ensure that the output is what the user would anticipate.

If you want truly interactive programs, you use a C language library that offers interactive functions. The NCurses library is great because it has both input and output functions that let you create full-screen text programs that are immediately interactive.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.