Lesson goal: Reading from a data file

Previous: Introduction to data files | Home | Next: Reading all data from a file (1)

In the last lesson, you learned how to open a file. Now, let's learn how to read in some data contained in the file.

To read data from a file, you can used the file_read function, that works like this:

Move the mouse over a dotted box for more information.

Now you try. Run the code and see if you can read the first line of data from the file sample.dat.

Type your code here:

See your results here:

This code will not run! Fill in the file name in the file_open line, and what will you pass in to the file_read function?

If everything works, you should see a number appear on your screen, which is the first number in the file sample.dat. Dismiss.

Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.