Small Basic - File Error Example: Writing to a Read-Only File


File Error Example: Writing to a Read-Only File

NOTE: This article points back to the "Learn to Program with Small Basic" book. The references to chapter listings refer to that book.

===========

Files have different attributes (such as Read-only, and Hidden). The Read-only attribute means the file can be read but not edited or saved over; it can be used to protect a file from unintentional change. Let’s see how the read-only property can affect your program.

First, modify line 2 of Listing 19-4 so that the specified path’s valid (it exists). In our case, we made this change:

path = "C:\Temp\Out.txt"

Then run the program to create the output file. After that, go to the c:\Temp folder, right-click the Out.txt file, and click Properties in the popup menu that appears. You’ll see a dialog that lets you view and update the file’s attributes (see Figure 19-5). Check the Read-only check box and click OK to apply your changes.

Figure: A part of the file properties dialog

Now run the program again. This time you should see an error message similar to the following:

FAILED: Access to the path 'C:\Temp\Out.txt' is denied.

 

TRY IT OUT!

Remove the Read-only property from Out.txt, and then start Microsoft Excel and open the file. Then run the program again. You should see an error similar to the following:

FAILED: The process cannot access the file 'C:\Temp\Out.txt' because it is being used by another process.

 

Note: Most applications open files in “shared” mode, which means that other apps can still read from (and sometimes write to) them when are open. However, Excel puts an exclusive lock on files when it opens them.

 

 

Do you have any questions? Ask us! We're full of answers and other fine things!

Head to the Small Basic forum to get the most answers to your questions: 

http://social.msdn.microsoft.com/Forums/en-US/smallbasic/threads/   

And go to http://blogs.msdn.com/SmallBasic to download Small Basic and learn all about it!

Have a Small and Basic day,

   - Ninja Ed & Majed Marji

Comments (1)

Skip to main content