3.1 file input and output

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

Upload: allenbailey

Post on 23-Dec-2014

900 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 3.1   file input and output

Microsoft® Small Basic

File Input and Output

Estimated time to complete this lesson: 1 hour

Page 2: 3.1   file input and output

File Input and Output

In this lesson, you will learn about:

Using different properties of the File object.

Using different operations of the File object.

Page 3: 3.1   file input and output

The File Object

The File object includes various operations and properties, such as:

A computer file is a collection of data stored by your computer. Small Basic programming allows you to work with external files from your program.

With the help of the File object in Small Basic, you can access information from a file 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: 3.1   file input and output

Operations of the File Object

As you see, the File object provides many ways of working with files.Let’s learn about some operations of the File object…

WriteLine

You can use the WriteLine operation to write a line of text at the specified line number in a file.

AppendContents

With the AppendContents operation, you can add the specified text at the end of a file.

ReadContents

Use the ReadContents operation to read the entire contents of a specified file.

Page 5: 3.1   file input and output

Operations of the File Object

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

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

Page 6: 3.1   file input and output

Operations of the File Object

CopyFile

You can use the CopyFile operation to copy the specified file to a destination.

GetFiles

With the GetFiles operation, you can get a list of all the files present in the specified directory.

Page 7: 3.1   file input and output

Operations of the File Object

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

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

OUTPUT

Page 8: 3.1   file input and output

Operations of the File Object

CreateDirectory

Use the CreateDirectory operation to create the specified directory at the desired location.

GetDirectories

With the GetDirectories operation, you can get the path of all the directories in the specified directory path.

Page 9: 3.1   file input and output

Operations of the File Object

Let’s see how we can apply these operations…

As you see, you first use the CreateDirectory operation to create a directory.

Next, you use the GetDirectories operation to get the path of the all the directories present in the specified location.

OUTPUT

Page 10: 3.1   file input and output

OUTPUT

The LastError Property

With the help of the LastError property, you can get details of the last file-operation-based error in your program. This property is quite useful when you can’t execute a file operation in your program because of some error.

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

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

Page 11: 3.1   file input and output

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: 3.1   file input and output

It’s Time to Apply Your Learning…

Write a program by performing the following steps:

Create a directory by accepting a suitable name from the user.

Download a file from the network and copy it to the created directory.

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

Accept additional content from the user and add that content to the file.

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