This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Input / Output
.NET 1.1+

Creating Fixed-Width Data Files

Fixed-width data files are an alternative to comma-separated values (CSV) for storing and sharing tabular data. The information in a fixed-width data set tends to use more storage or bandwidth than CSV but is more readable to humans.

WriteLine Method

The key method for the FixedWidthWriter class is WriteLine. This outputs a full line of fixed-width data to a stream or file. The data is provided as an array of strings. The array must have the same number or fewer elements than the Widths property.

The method loops through the elements in the data array, outputting each using the WriteField method, which we'll add shortly. Each datum is passed to WriteField along with the corresponding column width. Once all fields have been outputted, the method calls the base class's WriteLine method to terminate the current line in readiness for the next.

public void WriteLine(string[] data)
    if (data.Length > Widths.Length)
        throw new InvalidOperationException("The data has too many elements.");

    for (int i = 0; i<data.Length; i++)
        WriteField(data[i], Widths[i]);

To complete the class we'll add the WriteField method. This formats a single element to ensure that it is the correct length, then outputs it using the standard Write method. The method starts by converting the string data to a character array. This isn't technically necessary but it makes it easy to work with the data.

If the character array is too large for the available area, it is truncated using the overload of the Write method that permits a starting point and a length to be specified. If the data is not too long, it is outputted before writing the correct number of non-data characters to fill the specified width.

private void WriteField(string datum, int width)
    char[] characters = datum.ToCharArray();
    if (characters.Length > width)
        Write(characters, 0, width); 
        Write(new string(NonDataCharacter, width - characters.Length));

Testing the Class

We can now try out the class. The following code, added to the Main method, defines some data and outputs it to a file. You should change the output path to select a suitable folder and file name for your system.

int[] widths = new int[] { 20, 7, 6 };
string[] headers = new string[] { "Company", "Spend", "Rating" };
string[] company1 = new string[] { "Quality Foods Limited", "1000000", "Gold" };
string[] company2 = new string[] { "The Pieman", "50000", "Silver" };
string[] company3 = new string[] { "Bill's Fruit and Veg", "10000", "Bronze" };

using (FixedWidthWriter writer = new FixedWidthWriter(widths, "d:\\test.txt"))

The contents of the new file are shown below. You can see that the fixed column widths are as defined in the widths array. Some of the data is padded to fill the correct width, whilst other elements are truncated. Try re-running the program with different column widths and alternative padding characters to see the effects.

Company             Spend  Rating
Quality Foods Limite1000000Gold  
The Pieman          50000  Silver
Bill's Fruit and Veg10000  Bronze
4 May 2013