3

I defined both FixedLength and Delimited attributes on a single class. It reads the fixed length file fine but fails when i try to write out a csv.

[FixedLengthRecord]
[DelimitedRecord(",")]
public class PCFFileHeader
{        
    [FieldFixedLength(2)] public string RecordType { get; set; }
    [FieldFixedLength(25)] public string FileDescription { get; set; }
}

var engine = new MultiRecordEngine(typeof(PCFFileHeader), typeof(SecondHeader));

engine.RecordSelector = RecordSelector;
var output = engine.ReadFile(filePath);

// code to extract PCFHeader from output and create a list

var headerEngine = new DelimitedFileEngine<PCFFileHeader>();
headerEngine.WriteFile("header_" + DateTime.Now.ToString("yyyyMMddhhmmss"), pcfFileHeaderList);
shamp00
  • 11,106
  • 4
  • 38
  • 81

1 Answers1

0

You can't add both [FixedLengthRecord] and [DelimitedRecord] to the same class. FileHelpers will just treat it as [FixedLengthRecord]. With your current class, it will error if you do:

// This causes an exception because the class is not a delimited record definition
var delimitedHeaderEngine = new DelimitedFileEngine<PCFFileHeader>();

Instead, you should have two PCFFileHeader definitions, FixedPCFFileHeader and DelimitedPCFFileHeader.

[FixedLengthRecord]
public class FixedPCFFileHeader
{
    [FieldFixedLength(2)] public string RecordType { get; set; }
    [FieldFixedLength(25)] public string FileDescription { get; set; }
}

[DelimitedRecord(",")]
public class DelimitedPCFFileHeader
{
    public string RecordType { get; set; }
    public string FileDescription { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        var fixedHeaderEngine = new FileHelperEngine<FixedPCFFileHeader>();
        var recordsFixed = fixedHeaderEngine.ReadString("AADescription              ");
        Debug.Assert(recordsFixed.Count() == 1);
        Debug.Assert(recordsFixed[0].RecordType == "AA");
        Debug.Assert(recordsFixed[0].FileDescription == "Description              ");

        var delimitedHeaderEngine = new FileHelperEngine<DelimitedPCFFileHeader>();
        var recordsDelimited = delimitedHeaderEngine.ReadString("AA,Description");
        Debug.Assert(recordsDelimited.Count() == 1);
        Debug.Assert(recordsDelimited[0].RecordType == "AA");
        Debug.Assert(recordsDelimited[0].FileDescription == "Description");

        var pcfFileHeaderList = new FixedPCFFileHeader[] { new FixedPCFFileHeader() { RecordType = "AA", FileDescription = "Description" } };
        
        // If you want to output a header when exporting uncomment the following
        //headerEngine.HeaderText = "header_" + DateTime.Now.ToString("yyyyMMddhhmmss");
        
        var output = fixedHeaderEngine.WriteString(pcfFileHeaderList);
        Debug.Assert(output == "AADescription              \r\n");

        Console.WriteLine("All OK");
        Console.ReadLine();
    }
}

If you want to automatically detect the correct format during import, you can use the FileHelpers Smart Format Detector.

shamp00
  • 11,106
  • 4
  • 38
  • 81