DBF Extractor - C# Programming Exercise

This exercise involves creating a program that displays the list of fields stored in a DBF file. The DBF format is used by the old dBase database manager and is still supported as an export format by many current tools, such as Excel or Access.
DBF files are divided into two parts: a header that stores information about the structure of the file and a data zone. The header is divided into two parts: the first occupies 32 bytes and contains general information about the file, while the second contains information about each field and consists of as many 32-byte blocks as there are fields in the table.
To display the field information from the DBF file, the program must read both the general header zone and the field headers, analyzing the sizes and positions described to extract the names and types of each field.

 Category

File Management

 Exercise

DBF Extractor

 Objective

Create a program that displays the list of fields stored in a DBF file.

The DBF format is used by the old dBase database manager and is still supported as an export format by many current tools, such as Excel or Access.

DBF files are divided into two parts: a header that stores information about the structure of the file and a data zone.

The header zone is separated from the data zone with the CR character (carriage return, number 13 of the ASCII code). The header is divided into two parts: the first occupies 32 bytes and contains general information about the file, while the second contains information about each field and is made up of as many blocks of 32 bytes as there are fields in the table.

The general header of the file is:

Position Size (bytes) Description
1 1 Number that identifies the product with which the table was created
2 3 Last update date in the format of year/month/day
5 4 Total number of records in the table (in reverse order)
9 2 Total length of the header, including CR
11 2 Length of each record, including the delete flag character
13 2 Reserved
15 1 Active Transaction Flag
16 1 Encryption Flag
17 12 Indicators for local area network use
29 1 Flag indicating whether the file has an associated .MDX file
30 3 Reserved

The header of each field is:

Position Size (bytes) Description
1 11 Field Name
12 1 Field Type (C, D, F, L, M, N)
13 4 Reserved
17 1 Field Length
18 1 Number of decimal places if the field is numeric; also used for large character fields
19 2 Reserved
21 1 Work area Flag
22 10 Reserved
32 1 Flag indicating inclusion in the index .MDX

(Note that the number of fields is not indicated in the header, but it can be deduced by knowing the length of the header, which is in positions 9 and 10, and the size of each header block, which is 32 bytes.)

 Write Your C# Exercise

// Importing necessary namespaces
using System;
using System.IO;
using System.Text;

class DBFExtractor
{
    // Main method where the program execution begins
    static void Main()
    {
        // File path (change this to the DBF file you want to extract fields from)
        string filePath = "example.dbf";

        // Call the ExtractFields method to display the list of fields from the DBF file
        ExtractFields(filePath);
    }

    // Method to extract and display the list of fields from the DBF file
    static void ExtractFields(string filePath)
    {
        try
        {
            // Open the DBF file in binary read mode
            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                // Read the general header (32 bytes)
                byte[] header = new byte[32];
                fs.Read(header, 0, 32);

                // Extract the total length of the header (in bytes) from positions 9-10 (little-endian)
                int headerLength = BitConverter.ToInt16(header, 9);

                // The number of fields can be deduced from the header length
                int fieldCount = (headerLength - 32) / 32;

                // Skip to the start of the field headers
                fs.Seek(32, SeekOrigin.Begin);

                // Read each field header (32 bytes per field)
                for (int i = 0; i < fieldCount; i++)
                {
                    byte[] fieldHeader = new byte[32];
                    fs.Read(fieldHeader, 0, 32);

                    // Extract field name (first 11 bytes)
                    string fieldName = Encoding.ASCII.GetString(fieldHeader, 0, 11).Trim();

                    // Extract field type (12th byte)
                    char fieldType = (char)fieldHeader[11];

                    // Extract field length (17th byte)
                    byte fieldLength = fieldHeader[16];

                    // Display the extracted field information
                    Console.WriteLine($"Field {i + 1}: Name = {fieldName}, Type = {fieldType}, Length = {fieldLength}");
                }
            }
        }
        catch (Exception ex)
        {
            // Catch any errors (like file not found) and display the error message
            Console.WriteLine("Error: " + ex.Message);
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of File Management

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Text censorer

    This exercise involves creating a program to censor text files. The program should read a text file and output its contents to a new file, replacing certain words with "[CEN...

  •  SQL to text

    In this exercise, you need to create a C# program capable of parsing SQL INSERT commands and extracting their data into separate lines of text. The program should pro...

  •  PGM viewer

    The PGM format is one of the versions of the NetPBM image formats. Specifically, it is the variant capable of handling images in shades of gray. Its header starts with a line conta...

  •  Display BMP on console V2

    In this exercise, you are asked to develop a program in C# that can display a 72x24 BMP file on the console. To do this, you must use the information contained...

  •  Writing to a text file

    In this exercise of C#, you need to create a program that asks the user for several sentences (until they just press Enter without typing anything) and stores those s...

  •  Appending to a text file

    In this exercise of C#, you need to create a program that asks the user for several sentences (until they just press Enter without typing anything) and stores those s...