Hello, and welcome back.
We are in a new section.
This section is all about file input and output, writing
and reading from files.
We're going to concentrate on text files, but we'll talk about
binary files a bit as well.
In this lecture, I just want to provide an overview, tell you what
a file is and tell you how it's good use for storing persistent data.
So up until this point, all the data that our program
accesses is via memory.
We're familiar with this concept of variables, structures,
whatever it is, arrays.
All the data that your program uses is in memory.
And the reason that it's stored in memory is because
memory is really fast.
And you can access it quickly to perform calculations.
You're doing the fetch execute cycle all that stuff.
But it also is limited because the scope and variety of our
applications if it only uses memory, it's going to be somewhat limited.
We know that memory goes away when the program goes away, and
so that can be very limiting.
All serious business applications require more data than
would fit into main memory.
Main memory is also very small.
Nowadays, you can get a lot of memory, 16, 32 gigabytes.
But in the past and especially on embedded devices, you
don't have as much ram.
And as a result, your applications are very limited
because of space and size.
You really only want to store variables and things
like that in main memory.
It also because main memory goes away.
When the program goes away, you can't have any data that is
stored persistent across reboots.
So it all depends on the ability to process data that is persistent and
stored on an external device such as a hard disk drive, these big programs.
So you want to have this ability to store on the hard drive.
It's a little bit slower, but you can store a lot of data on there, and big
programs need to store a lot of data.
C provides many functions in the header file standardio.h for writing
to and reading from external devices.
You don't have to write your own code to read and write the files.
But you can see the advantage that applications offer that have the
ability to store data for a longer amount of time, a lot of advantages.
The external device you would use for storing and
retrieving data is typically the hard drive, the disk drive.
However, the header file functions that we're going
to talk about, you can use on any external storage device.
So you could use it on a USB drive.
You could use it on any external device that stores
data, an SSD drive, anything.
With all the examples up until now, any data that the user enters
is lost once the program's ends.
This is referred to as volatile memory.
Whenever you heard that term volatile memory, it means that
the memory is not persistent or the data is not persistent.
It goes away when the program ends or if the machine reboots.
So if the user wants to run the program with the same data,
he has to enter it again each time so that it gets restored.
It's very inconvenient.
It limits programming.
And again, this is volatile memory.
When you store stuff in files and you store it on the hard
drive, this is permanent storage.
It's still there even when you reboot the machine or when
your program is not running.
That's referred to as non-volatile memory, because it stays around.
A file can store non-volatile data and is usually stored on the disk or
nowadays a solid state drive an SSD, it's a named section of storage and
the standard ioh is a file containing the use of information in order to be
able to read and write to those files.
But we're going to use this concept of writing and reading data from
a file for permanent storage.
And you're going to notice how your applications become much more useful,
much more robust, much more scalable because now we have the ability
to store large amounts of data.
And we also do not have to worry about the data going away whenever the
program ends or the computer reboots.
So it's going to be very, very advantageous to use files.
A file on the linux operating system or windows operating
system is stored a certain way.
But as far as c goes to programming language, it's just
a sequence of bytes, and each byte can be read individually.
This corresponds to the file structure in unix, but it only makes
a difference when you're reading text files and windows because you
may have extra characters that you have to worry about concerning size,
but it's just a sequence of bytes.
So if you have a text file and it has more bytes, you can still read those
bytes you just have to understand what those bytes are representing.
So here's what a a file looks like and how we're going to
read it and write to it using the c programming language.
It's just a series of bytes with different offsets.
The beginning of the file would be by zero.
And then you can always find different positions in the file, could be 4
bytes into, could be 100 bytes into it, and then you have the end of file.
And the end of file is usually marked with a special marker
called eof or end of file.
So file has a beginning and an end and a current position.
The current position is defined so many bytes from the beginning.
The current position is where any file action will take place.
You're going to write or you're going to read at that position.
And we're going to also talk about some functions where you
can move around to move the current position anywhere you
want in a file even at the end.
If you want to read at the end or write to the end.
There are two types of files.
There are text files and binary files.
A text file is data written as a sequence of characters,
organized as lines.
Each line ends with a new line.
And the text data can get vary from system to system depending
on how characters are interpreted.
So for example on windows and dos, every line has a carriage
return and a line feed, so a backslash r and a backslash n.
And so when you read the data or write the data, you have
to understand this because the format's a little different.
But on linux, you don't have a backslash r.
So you have to understand how the text data is written
even though it's organized by lines and its series of bytes.
Binary data is written as a series of bytes exactly
as they appear in memory.
So examples of files that have binary data are files like
image data or music encoding.
Binary files are not readable if you were to try to bring them up in a text
editor because they are just bytes.
We're going to concentrate on text files in this section.
But you can write any data you like to a file.
You can write it as a series of bytes and how they appear in
memory or you can run it as text.
But once the file has actually been written, it's just going to
consist of a series of bytes even if it's represented as type text.
You can view text files in an editor which makes it more readable.
You have to understand that the format of the file in order to read it.
And this is what I mentioned with if you're running on dos, windows
or linux, where windows may have extra characters when it writes data.
So a sequence of 12 bytes in a binary file could be 12 characters.
It could be 12 bytes signed integers or 12 bit unsigned integers.
Binary files are especially tricky because they just store
as they appear in memory.
So in binary mode, each and every byte of the file is accessible.
But in order to read something from a file, you have to
understand what's in there as far as data and how it's stored.
And it may depend on what system you're running on,
and it's going to also depend whether it's text or binary.
Streams.
C programs are automatically open on three files on your behalf.
A stream can represent a file, but a stream is a more generic term that
can represent any kind of input.
So it could represent the keyboard, it could represent the
console, it could represent a file, it could represent a socket.
If you're used to networking, streams are a more generic term.
And there's three streams that are opened up automatically for
you in the c programming language.
One is the standard input.
And this is the normal input device of your system.
Usually by default it's the keyboard.
We've been using the standard input when we do scanf.
But you could actually use scanf to read from files if you change
the input stream, maybe you change the input stream to a file.
Standard output.
This usually displays on the screen usually via the console.
Standard error is also on the screen, but you usually have
to redirect it in order to see it using like the ampersand
three or something like that.
But these are the three outputs.
I mentioned standard input is the file that is read by get char and scanf.
Standard output is used by put char, put s and printf.
We've mainly been using printf.
You can also redirect files on the system to be recognized
as a standard input output.
This is redirection, this is an operating system concept where you can
redirect data, send the output to a file, for example, or get input from
a file as opposed to standard archive.
This is where you can use the less than or greater than symbols.
The purpose of the standard error output is to provide a logically
distinct place to send error messages, so it's kind of separate.
Even though it's displayed to the screen to access it you
have to have special command.
A stream is an abstract representation of any external
source or destination for data.
This is why it's more generic than say a file.
The keyboard, the command line on your display files on a disk, those are all
examples of how you work with streams.
Streams are going to be much more important when you start studying c++.
The c library provides functions for reading and writing
to or from data streams.
So you can use the same input output functions for reading
and writing any external device that is mapped to a stream.
That's why I mentioned you can use scanf to read from the keyboard.
You could also use scanf to read from files.
It all depends on what the input or where the stream is pointing to.
And so the functions are very, very useful and very,
very flexible to do this.
And so this is just a high level overview of what a file is and
why would use it, how it is persistent data, it's non-volatile
memory, it stays around.
We're going to use it, and we're going to use the standard IO header
file to read and write to it when we need to store data for our program.
And our programs end up being a lot more robust, a lot more
usable, a lot more flexible.
So in our next lecture, we'll talk about actual implementing this,
using the functions in the standard IO header. Thank you.