microsoft® small basic file input and output estimated time to complete this lesson: 1 hour

12
Microsoft® Small Basic File Input and Output Estimated time to complete this lesson: 1 hour

Upload: dion-brayton

Post on 31-Mar-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Microsoft® Small Basic File Input and Output Estimated time to complete this lesson: 1 hour

Microsoft® Small Basic

File Input and Output

Estimated time to complete this lesson: 1 hour

Page 2: Microsoft® Small Basic File Input and Output Estimated time to complete this lesson: 1 hour

File Input and Output

In this lesson, you will learn how to:

Use different properties of the File object.

Use different operations of the File object.

Page 3: Microsoft® Small Basic File Input and Output Estimated time to complete this lesson: 1 hour

The File Object

The File object includes the following operations and properties:

A computer file is a collection of data that your computer stores. In Small Basic, you can work with external files from your program.

By using the File object in Small Basic, you can access information from a file that is stored on your computer. You can also read and write information from and to the file.

CreateDirectory WriteLine

AppendContents CopyFile

GetFiles DeleteDirectory

GetDirectories

ReadContents

LastError

Page 4: Microsoft® Small Basic File Input and Output Estimated time to complete this lesson: 1 hour

Operations of the File Object

As you see, you can work with files in many ways by using the File object.Let’s learn about some operations of the File object…

WriteLine

You can write a line of text at a line number that you specify in a file by using the WriteLine operation.

AppendContents

You can add text that you specify at the end of a file by using the AppendContents operation.

ReadContents

You can read the entire contents of a file by using the ReadContents operation.

Page 5: Microsoft® Small Basic File Input and Output Estimated time to complete this lesson: 1 hour

Operations of the File Object

Let’s write a program to gain better understanding of these operations.

OUTPUTIn this example, you specify the path of a file and write a sentence to it by using the WriteLine operation. Next, you add a sentence to the existing content by using the AppendContents operation. Finally, you read the entire contents of the file by using the ReadContents operation.

Page 6: Microsoft® Small Basic File Input and Output Estimated time to complete this lesson: 1 hour

Operations of the File Object

CopyFile

You can copy the specified file to a destination by using the CopyFileoperation.

GetFiles

You can get a list of all the files in a directory that you specify by using the GetFiles operation.

Page 7: Microsoft® Small Basic File Input and Output Estimated time to complete this lesson: 1 hour

Operations of the File Object

Let’s write a program to better understand these operations.

In this example, you copy the specified source file to the specified destination by using the CopyFile operation. You also specify the directory path, and you then display the paths of all files in the output window by using the GetFiles operation.

OUTPUT

Page 8: Microsoft® Small Basic File Input and Output Estimated time to complete this lesson: 1 hour

Operations of the File Object

CreateDirectory

By using this operation, you can create a directory with a name that you specify at a location that you specify.

GetDirectories

By using this operation, you can get the paths of all the directories in the directory path that you specify.

Page 9: Microsoft® Small Basic File Input and Output Estimated time to complete this lesson: 1 hour

Operations of the File Object

Let’s see how we can apply these operations…

First, you create a directory by using the CreateDirectory operation.

Next, you get the path of all the directories in the location that you specify by using the GetDirectories operation.

OUTPUT

Page 10: Microsoft® Small Basic File Input and Output Estimated time to complete this lesson: 1 hour

OUTPUT

The LastError Property

By using the LastError property, you can get details about the most recent file-operation error that occurred in your program. This property is quite useful when an error prevents your program from performing a file operation.

In this example, you write text to a file at a specific line number that you specify by using the WriteLine operation of the File object.

Next you get the details of the actual error in the program, if any, by using the LastError property of the File object.

Page 11: Microsoft® Small Basic File Input and Output Estimated time to complete this lesson: 1 hour

Let’s Summarize…

Congratulations! Now you know how to:

Use different properties of the File object.

Use different operations of the File object.

Page 12: Microsoft® Small Basic File Input and Output Estimated time to complete this lesson: 1 hour

Show What You Know

Write a program that performs the following steps:

Requests a suitable name for a directory from the user, and creates a directory of that name.

Downloads a file from the network, and copies it to the new directory.

Displays the contents of the downloaded file in the text window.

Accepts additional content from the user, and adds it to the file.

Displays the final content from the file in the text window.