Introduction to File Processing


Overview of File Processing and Definitions



A piece of information used in an application is primarily represented as a group of bits. So far, if we requested information from the user, when the application exited, we lost all information that the user had entered. This is because such information was only temporarily stored in the random access memory (RAM). In some cases, you will want to "keep" information that the user has entered so you can make the information available the next time the user opens the application. In some other cases, whether you request information from the user or inherently provide it to the user, you may want different people working from different computers to use or share the same data. In these and other scenarios, you must store the information somewhere and retrieve it when necessary. This is the basis of file processing.



A file is a series of bytes of data that are arranged in a particular manner to produce a usable document. For easy storage, location, and management, the bytes are stored on a medium such as a hard disc, a floppy disc, a compact disc, or any valid and supported type of storage. When these bytes belong to a single but common entity and hold values that are stored on a medium, the group is referred to as a file.

For greater management, files can be stored in a parent object called a directory or a folder. Since a file is a unit of storage and it stores information, it has a size, which is the number of bits it uses to store its values. To manage it, a file has a location also called a path that specifies where and/or how the file can be retrieved. Also, for better management, a file has attributes (characteristics) that indicate what can be done on the file or that provide specific information that the programmer or the operating system can use when dealing with the file.


File processing consists of creating, storing, and/or retrieving the contents of a file from a recognizable medium. For example, it is used to save word-processed files to a hard drive, to store a presentation on floppy disk, or to open a file from a CD-ROM. A stream is the technique or means of performing file processing. In order to manage files stored in a computer, each file must be able to provide basic pieces of information about itself. This basic information is specified when the file is created but can change during the lifetime of a file.

To create a file, a user must first decide where it would be located: this is a requirement. A file can be located on the root drive. Alternatively, a file can be positioned inside of an existing folder. Based on security settings, a user may not be able to create a file just anywhere in the (file system of the) computer. Once the user has decided where the file would reside, there are various means of creating files that the users are trained to use. When creating a file, the user must give it a name following the rules of the operating system combined with those of the file system. The most fundamental piece of information a file must have is a name.

Once the user has created a file, whether the file is empty or not, the operating system assigns basic pieces of information to it. Once a file is created, it can be opened, updated, modified, renamed, etc.

Streaming Prerequisites



To support file processing, the .NET Framework provides the System.IO namespace that contains many different classes to handle almost any type of file operation you may need to perform. Therefore, to perform file processing, you can include the System.IO namespace in your project.

The parent class of file processing is Stream. With Stream, you can store data to a stream or you can retrieve data from a stream. Stream is an abstract class, which means you cannot use it to declare a variable in your application. As an abstract class, Stream is used as the parent of the classes that actually implement the necessary operations. You will usually use a combination of classes to perform a typical operation. For example, some classes are used to create a stream object while some others are used to write data to the created stream.

Practical LearningPractical Learning: Introducing Streaming

  1. Start Microsoft Visual Basic or Visual Studio and create a Windows Application named ClarksvilleIceCream2
  2. In the Solution Explorer, right-click Form1.vb and click Rename
  3. Type Exercise.vb and press Enter twice
  4. In the Properties window, change the form's Text to Ice Cream Vending Machine
  5. Design the form as follows:
    Ice Cream Vending Machine
    Control Name Text Additional Properties
    Label   Order Date:  
    DateTimePicker dtpOrderDate   Format: Short
    Label   Order Time:  
    DateTimePicker dtpOrderTime   Format: Time
    ShowUpDown: True
    Label   Flavor:  
    ComboBox cboFlavors   DropDownStyle: DropDownList
    Label   Container:  
    ComboBox cboContainers   DropDownStyle: DropDownList
    Label   Ingredient:  
    ComboBox cboIngredients   DropDownStyle: DropDownList
    Label   Scoops:  
    TextBox txtScoops 1 TextAlign: Right
    Label   Order Total:  
    TextBox txtOrderTotal 0.00 TextAlign: Right
    Button btnClose Close Click to end
  6. Click the combo box to the right of the Flavor label. Then, in the Properties, click the ellipsis button Ellipsis of Items property and create the list with:
    Cream of Cocoa
    Chocolate Chip
    Cherry Coke
    Butter Pecan
    Chocolate Cookie
    Chunky Butter
    Organic Strawberry
    Chocolate Brownies
    Caramel Au Lait
  7. Click OK
  8. Click the combo box to the right of the Container label. Then, in the Properties, click the ellipsis button Ellipsis of Items property and create the list with:
  9. Click OK
  10. Click the combo box to the right of the Ingredient label. Then, in the Properties, click the ellipsis button Ellipsis of Items property and create the list with:
    Mixed Nuts
    M & M
  11. Click OK
  12. Right-click the form and click View Code
  13. In the Class Name combo box, select txtScoops
  14. In the Method Name combo box, select Leave and implement the event as follows:
    Private Sub txtScoops_Leave(ByVal sender As Object, _
                                    ByVal e As System.EventArgs) _
                                    Handles txtScoops.Leave
            Dim PriceContainer As Double
            Dim PriceIngredient As Double
            Dim PriceScoops As Double
            Dim OrderTotal As Double
            Dim NumberOfScoops As Integer = 1
            ' The price of a container depends on which one the customer selected
            If cboContainers.Text = "Cone" Then
                PriceContainer = 0.55
            ElseIf cboContainers.Text = "Cup" Then
                PriceContainer = 0.75
                PriceContainer = 1.15
            End If
            ' Find out if the customer wants any ingredient at all
            If cboIngredients.Text = "None" Then
                PriceIngredient = 0.0
                PriceIngredient = 0.95
            End If
                ' Get the number of scoops
                NumberOfScoops = CInt(txtScoops.Text)
                If NumberOfScoops = 1 Then
                    PriceScoops = 1.85
                ElseIf (NumberOfScoops = 2) Then
                    PriceScoops = 2.55
                Else ' if( NumberOfScoops= 3 )
                    PriceScoops = 3.25
                End If
                ' Make sure the user selected a flavor, 
                ' otherwise, there is no reason to process an order
                If cboFlavors.Text <> "" Then
                    OrderTotal = PriceScoops + PriceContainer + PriceIngredient
                    txtOrderTotal.Text = OrderTotal.ToString("F")
                End If
            Catch ex As Exception
                MsgBox("The value you entered for the scoops is not valid" & _
                                 vbCrLf & "Only natural numbers such as 1," & _
                                 vbCrLf & " 2, or 3 are allowed" & _
                                 vbCrLf & "Please try again")
            End Try
    End Sub
  15. Execute the application. Here is an example:
    Clarksville Ice Cream
  16. Close the form and return to Visual Studio

The Name of a File

Before performing file processing, one of your early decisions will consist of specifying the type of operation you want the user to perform. For example, the user may want to create a brand new file, open an existing file, or perform a routine operation on a file. In all or most cases, whether you are creating a new file or manipulating an existing one, you must specify the name of the file. You can do this by declaring a String variable but, as we will learn later on, most classes used to create a stream can take a string that represents the file.

If you are creating a new file, there are certainly some rules you must observe. The name of a file follows the directives of the operating system. On MS DOS and Windows 3.X (that is, prior to Microsoft Windows 9X), the file had to use the 8.3 format. The actual name had to have a maximum of 8 characters with restrictions on the characters that could be used. The user also had to specify three characters after a period. The three characters, known as the file extension, were used by the operating system to classify the file. That was all necessary for those 8-bit and 16-bit operating systems. Various rules have changed. For example, the names of folders and files on Microsoft Windows >= 95 can have up to 255 characters. The extension of the file is mostly left to the judgment of the programmer but the files are still using extensions. Applications can also be configured to save different types of files; that is, files with different extensions.

Author Note At the time of this writing, the rules for file names for Microsoft Windows were on the MSDN web site at Windows Development\Windows Base Services\Files and I/O\SDK Documentation\Storage\Storage Overview\File Management\Creating, Deleting, and Maintaining Files\Naming a File (because it is a web site and not a book, its pages can change anytime).

Based on this, if you declare a String variable to hold the name of the file, you can simply initialize the variable with the necessary name and its extension. Here is an example:

File Processing

Private Sub btnSave_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                              Handles btnSave.Click
        Dim Filename As String = "Employees.spr"
End Sub 

Practical LearningPractical Learning: Specifying the Name of a File

  1. Right-click the form and click View Code
  2. Just above the Public Class line, import the System.IO namespace:
    Imports System.IO
    Public Class Exercise
  3. In the Class Name combo box, select btnClose
  4. In the Method Name combo box, select Click and implement the event as follows:
    Private Sub btnClose_Click(ByVal sender As Object, _
                                   ByVal e As System.EventArgs) _
                                   Handles btnClose.Click
            Dim answer As MsgBoxResult = _
                MsgBox("Do you want to save this order to remember it " & _
                                "the next time you come to " & _
                                         "get your ice cream?", _
                       MsgBoxStyle.YesNo Or MsgBoxStyle.Question, _
                       "Ice Cream Vending Machine")
            If answer = MsgBoxResult.Yes Then
                Dim Filename As String = InputBox( _
                    "Please type your initials and press Enter", _
                    "Ice Cream Vending Machine", "AA", 100, 100)
                If Filename <> "" Then
                    ' Wonderful
                    MsgBox("The ice cream order will not be saved")
                End If
            End If
            MsgBox("Good Bye: It was a delight serving you")
    End Sub
  5. Scroll up in the file and, under the other using lines, type Imports System.IO

The Path to a File

If you declare a string as above, the file will be created in the folder as the application. Otherwise, you can create your new file anywhere in the hard drive. To do that, you must provide a complete path where the file will reside. A path is a string that specifies the drive (such as A:, C:, or D:). The sections of a complete path string are separated by a backslash. For example, a path can the made of a folder followed by the name of the file. An example would be


A path can also consist of a drive followed by the name of the folder in which the file will be created. Here is an example:

C:\Program Files\Palermo.tde

A path can also indicate that the file will be created in a folder that itself is inside of another folder. In this case, remember that the names of folder must be separated by backslashes.

When providing a path to the file, you could encounter different types of problems:

  • If the drive you specify does not exist or cannot be read, the compiler would consider that the file does not exist
  • If you provide folders that do not exist in the drive, the compiler would consider that the file does not exist. This also means that the compiler will not create the folder(s) (the .NET Framework provides all means to create a folder but you must ask the compiler to create it; simply specifying a folder that does not exist will not automatically create it, even if you are creating a new file).

Therefore, it is your responsibility to make sure that either the file or the path to the file is valid. As we will see in the next sections, the compiler can check the existence of a file or path. 


Home Copyright © 2008-2016, FunctionX, Inc. Next