- The C++ standard library provides some simple functions
for unformatted character IO.
This is working.CPP,
from chapter eight of the exercise files.
And you notice down here on line seven,
I'm using the puts(); function
to display the words, "Hello World," on the console.
And when I build and run this,
you'll notice that that's what it does.
It says, "Hello World," on the console.
And you notice that it says program ended
with exit code zero on the next line.
So after Hello World, it's actually putting out
a line feed or a new line.
If instead I use the fputs(); function
and fputs(); requires an output stream.
So here I say, stdout ;
which is the output stream for the console.
You'll notice that when I build and run this,
it says program ended on the same line
because fputs(); does not put out a new line.
I have to do that myself if I want one,
when I build and run now
it works exactly like it did with puts();.
So here's stdout ; is being used like a file handle.
And it's one of the standard file handles
that you don't have to open.
And that includes stdout ;, stdin ;, and stderr ;.
Of course, you can use this with any file handle.
As we saw in our lessons on file IO earlier in this chapter,
it's also a corresponding fgets ()
and fgets() takes a buffer and an integer,
which tells you how big the buffer is,
how much space there is for data in the buffer
and a file handle.
So before we finish this line of code here,
let's go ahead and declare a file handle.
I'm going to give it a size first
and call this size bufsize and 256 bytes.
And so static char buf{},
and we'll give it that bufsize for the size of the array.
So this is an array of characters.
And now here I can give it the buffer
and bufsize and for a file handle,
I'm going to use stdin ; and we'll go ahead
and we will type into this buffer.
And so first we'll put out a little prompt.
We'll say, fputs("prompt: ");
Like that, and then we're all done.
We'll say here's our output.
And we'll put the output on the console with fputs();
This one here. You see, it says no matching call
because fputs(); requires a file stream.
Now we're going to do something you haven't seen before.
We're going to flush our output buffer.
We're going to say fflush(stdout ;);
The reason for flushing,
the standard out all the file IO in C and C++
is considered asynchronous.
It may or may not happen when you tell it to happen.
It might happen a moment later or even minutes later.
And what fflush(); does is it says,
"okay, finish writing to this output stream now,
and I'll wait."
And so it's, what's called a block dial.
No code afterwards will happen until that flush is complete.
And so it usually just takes a moment.
I mean, obviously if you're writing, you know,
many megabytes to a large physical disc,
it might take more than a moment,
but for just writing text out to the console,
all this does is it makes sure that
what you write comes out to the console
in the order that you write it.
And so occasionally you're going to want to use
fflush(); like this.
So I'm going to go ahead
and I'm going to build and run this,
and here's my prompt.
And I'm just going to type a few characters here
and I'm going to press my return key.
And then here's my output. It says, "a few characters here."
And so you see what happened is fgets();
read from the console and it put it in the buffer.
And then I displayed the buffer using fputs();.
So just like has a corresponding puts();
that only works with stdout ;
and doesn't require a file handle.
fgets(); has a corresponding function called gets();
that only works with stdin ;.
And the problem is
because it does not check a buffer length.
It is completely unsafe
and you should never, ever, ever use gets(); .
You should always use fgets();
because it allows you to declare the size of your buffer
and this makes it safe.
There's a warning in the man page.
I'm going to show you that right now.
This is the standard BSD,
which is the Unix that Mac OS is based upon.
This is the standard BSD man page for fgets(); and gets();.
And if we come down here to the end,
you'll notice here under security considerations,
it says, "the gets(); function cannot be used securely
because of its lack of bounds checking,
et cetera, et cetera.
It is strongly suggested that fgets(); function
be used in all cases.
So don't ever use gets(); by itself.
You can always use fgets();
and you should always use fgets();
when you need to get input either from a file
or from the console.
So these are the basic unformatted character IO functions
in the C++ standard library.