Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
This does not let you write code that just reads files off your hard drive or saves files to your hard drive. But seeing as I believe this is an important subject to talk about in the Fundamentals course I'm going to talk a little bit about file input and output. I'm going to start with couple of pseudocode examples and then show what it might look like in another language, because here's the thing. File input/output, File I/O, can actually be a little tougher than it first sounds, because to read the most basic of files, we need to know the path, we need to open the file, we need to read the contents of the file, and we need to close it.
So if I just wrote this as simple pseudocode, we'd say open file at path myfile.txt. This is allowing us to grab hold of it, to treat it as ours. Then I could read the contents of the file into a string variable and then I do need to close the file. It's always a rule to close the file when you're done with it. But here is the issue. There is a whole bunch of ways this could go wrong. What if there is no file at that path? What if some other program has that file already open? What if they've already grabbed it? What if that file is huge? Can we just read it all into a string variable? And you'll often find that dealing with file input/output, there is a lot more conditional code going on just to check all the things that could possibly go wrong.
But let's say we're not going to worry about it. Let's say we just want to read in a small text file that we know exists. Well, here's an example using the programming language Ruby. And as usual, don't worry too much about the syntax. It's not what we're focusing on here. Just the overall approach. Can you scan this code and see where the file path is? Can you see where we're probably opening the file? Can you see where we close it? It's not about whether you could turn around and write this down yourself. It's about that you understand the general approach here.
So this will open the file called simpleexample.txt. Because I didn't put any other path information here, this file should be in the same folder as the program itself. Now the next line, we have File.open using that filename. And we're using this r in quotes here. This r means open this file in read-only mode. If we wanted to open this file so we could write to it, we could use w, or for both reading and writing, we could say r+.
Now it's very common for different programming languages to support opening files in different modes. One mode for reading, another mode for writing, another mode for read and write. You might have a mode for overwrite this if the file exists already or another mode for append to it if the file exists already. These are all very common. And we choose modes because it's helpful. Opening a file in read-only mode has less impact on the system and it allows other people to open it too.
We need the path, we need to open the file, we need to write the contents, we need to close the file. Now I will say here Ruby is one of the simplest languages for reading and writing files. If I'd shown you these examples in C++ or Java or C#, we would have had a lot more lines to deal with. Now there's a big difference between reading a typical text file and reading a file of binary data like audio or video. It's very common when you start working with files that you'll find yourself needing to break things up a bit.
In most languages you rarely read an entire file at once unless you know it's going to be small. Instead, you read the file one chunk at a time. You break it up into pieces. One example would be that rather than reading a text file all at once, you process it one line at a time. Some pseudocode here might be that we open the file at the path myfile.txt and then we're going to have a loop that says while the file has lines left, we will read one line, we'll display that, and then we'll end the loop.
We'll loop around again. Does it still have lines left? Yes, it does. We'll read another line, display that line, and just go piece by piece through the entire file. Then finally, we'll close the file. And if we're opening the file, we close the file. With many languages, even higher-level ones like C#, Java, and Objective-C, you can end up working with files by working with what are called streams. The idea of a stream is quite simple. It just means a stream of bytes like a conveyor belt of bytes. And it can sound complex, but it really isn't. When working with streams, we just get to take them one chunk at a time.
And they're great because they allow you to stop caring where this data actually is. You don't really mind anymore. Is it on a file on your hard drive? It could be. Maybe I'm reading it off a network, off a website. It doesn't matter. It's just a stream of data that I need to deal with. But a typical task for a programmer new to any language is finding out how that language likes to work with file input and output. You'll find that most languages deal with the same concepts in slightly different ways, but they all have substantial built-in functionality for reading and writing different kinds of files.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96870 Viewers
56 Video lessons · 110563 Viewers
71 Video lessons · 79398 Viewers
131 Video lessons · 38087 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.