Your SlideShare is downloading. ×
0
3.1   file input and output
3.1   file input and output
3.1   file input and output
3.1   file input and output
3.1   file input and output
3.1   file input and output
3.1   file input and output
3.1   file input and output
3.1   file input and output
3.1   file input and output
3.1   file input and output
3.1   file input and output
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

3.1 file input and output

1,056

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,056
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Using the File object, you can also save and open settings across various sessions of your program.
  • If the WriteLine operation is successful, it displays “SUCCESS” in the output window; otherwise, it displays “FAILED”. It will overwrite the previously written content on the specified line. Likewise, if the AppendContents operation is successful, it displays “SUCCESS” in the output window; it displays “FAILED” if unsuccessful. The ReadContents operation is faster when the size of the file is less than 1 MB. The operation slows down as the size of the file increases—especially for files larger than 10 MB. It displays the entire contents of the specified file on the output window.Code:Writeline:File.WriteLine("C:Small Basic.txt", 1, "Hello")AppendContents:File.AppendContents("C:Small Basic.txt","Take Care")ReadContents:File.ReadContents("C:Small Basic.txt”)
  • Code:FilePath = "C: empTempSubdirectorymy.txt"TextWindow.WriteLine("Write Content = " + File.WriteLine(FilePath, 1, "Shakespeare was a great writer."))TextWindow.WriteLine("Append Content = " + File.AppendContents(FilePath, "He wrote many plays."))TextWindow.WriteLine("Read Content = " + File.WriteLine(FilePath))
  • Code:FilePath = "C: empTempSubdirectmy.txt"TextWindow.WriteLine("Write Line Operation: " + File.WriteLine(FilePath, 1, "How are you?"))If File.LastError = "" ThenTextWindow.WriteLine("Operation Successful")ElseTextWindow.WriteLine(File.LastError)EndIf
  • Please Note: The file must exist at the specified network path for the solution to work.Solution:TextWindow.Write("Enter the name of the new directory: ")DirectoryName = TextWindow.Read()File.CreateDirectory(DirectoryName)filepath = "\mum-9785smShareFileIO.txt"downloadpath = Network.DownloadFile(filepath) If File.CopyFile(downloadpath, DirectoryName) = "SUCCESS" Then TextWindow.WriteLine("File has been downloaded from the network and copied to: " + DirectoryName)  files = File.GetFiles(DirectoryName) TextWindow.WriteLine("This is the content in the file: ") TextWindow.WriteLine(File.ReadContents(files[1])) TextWindow.Write("Enter data to be added in the file:")AppendedData = TextWindow.Read()File.AppendContents(files[1]," " + AppendedData)TextWindow.WriteLine("File content after adding data is as follows: ") TextWindow.WriteLine(File.ReadContents(files[1])) EndIf
  • Transcript

    • 1. Microsoft® Small Basic<br />File Input and Output<br />Estimated time to complete this lesson: 1 hour<br />
    • 2. File Input and Output<br />In this lesson, you will learn about:<br />Using different properties of the File object.<br />Using different operations of the File object. <br />
    • 3. The File Object<br />The File object includes various operations and properties, such as:<br />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. <br />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. <br />CreateDirectory<br />WriteLine<br />AppendContents<br />CopyFile<br />GetFiles<br />DeleteDirectory<br />GetDirectories<br />ReadContents<br />LastError<br />
    • 4. Operations of the File Object<br /><ul><li>WriteLine</li></ul>You can use the WriteLine operation to write a line of text at the specified line number in a file. <br /><ul><li>AppendContents</li></ul>With the AppendContents operation, you can add the specified text at the end of a file. <br /><ul><li>ReadContents</li></ul>Use the ReadContents operation to read the entire contents of a specified file.<br />As you see, the File object provides many ways of working with files.<br />Let’s learn about some operations of the File object…<br />
    • 5. Operations of the File Object<br />Let’s write a program to gain better understanding of these operations.<br />In this example, you specify a file path and use the WriteLineoperation 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.<br />output<br />
    • 6. Operations of the File Object<br /><ul><li>CopyFile</li></ul>You can use the CopyFile operation to copy the specified file to a destination. <br /><ul><li>GetFiles</li></ul>With the GetFiles operation, you can get a list of all the files present in the specified directory. <br />
    • 7. Operations of the File Object<br />Let’s write a program to better understand these operations.<br />output<br />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 GetFilesoperation to display the path of all files in the output window.<br />
    • 8. Operations of the File Object<br /><ul><li>CreateDirectory</li></ul>Use the CreateDirectory operation to create the specified directory at the desired location. <br /><ul><li>GetDirectories</li></ul>With the GetDirectoriesoperation, you can get the path of all the directories in the specified directory path. <br />
    • 9. Operations of the File Object<br />Let’s see how we can apply these operations…<br />output<br />As you see, you first use the CreateDirectory operation to create a directory. <br />Next, you use the GetDirectories operation to get the path of the all the directories present in the specified location. <br />
    • 10. The LastError Property<br />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.<br />In this example, you use the WriteLine operation of the File object to write text to the file at a specific line number.<br />Next you use the LastErrorproperty of the File object to get the details of the actual error in the program, if any.<br />output<br />
    • 11. Let’s Summarize…<br />Congratulations! Now you know how to:<br />Use different properties of the File object.<br />Use different operations of the File object.<br />
    • 12. It’s Time to Apply Your Learning…<br />Write a program by performing the following steps:<br /><ul><li>Create a directory by accepting a suitable name from the user.
    • 13. Download a file from the network and copy it to the created directory.
    • 14. Display the contents of the downloaded file in the text window.
    • 15. Accept additional content from the user and add that content to the file.
    • 16. Display the final content from the file in the text window.</li>

    ×