I have to read ragged right files is there any way to read them using file helpers library
my Code is like this
[FixedLengthRecord()]
class File_load
{
[FieldFixedLength(10)]
[FieldTrim(TrimMode.Right)]
public string proj_name;
[FieldFixedLength(30)]
[FieldTrim(TrimMode.Right)]
public string iso;
[FieldFixedLength(50)]
[FieldTrim(TrimMode.Right)]
public string line;
[FieldFixedLength(50)]
[FieldTrim(TrimMode.Right)]
public string pid;
}
if i don't use FieldFixedLength for last column it is throwing exception
Unhandled Exception: FileHelpers.BadUsageException: The record class marked with the FixedLengthRecord attribute must include a FixedLength attribute in each fi eld.
You can handle your scenario with the FixedMode.AllowLessChars
parameter of the FixedLengthRecord
attribute.
Here is a working program (I changed your field lengths to 2 for the example).
[FixedLengthRecord(FixedMode.AllowLessChars)]
class File_load
{
[FieldFixedLength(2)]
[FieldTrim(TrimMode.Right)]
public string proj_name;
[FieldFixedLength(2)]
[FieldTrim(TrimMode.Right)]
public string iso;
[FieldFixedLength(2)]
[FieldTrim(TrimMode.Right)]
public string line;
[FieldFixedLength(1000)]
[FieldTrim(TrimMode.Right)]
public string pid;
}
class Program
{
static void Main(string[] args)
{
var engine = new FileHelperEngine<File_load>();
var records = engine.ReadString(
"112233444444" + Environment.NewLine +
"1122334"
);
var firstRecord = records[0];
Assert.AreEqual("11", firstRecord.proj_name);
Assert.AreEqual("22", firstRecord.iso);
Assert.AreEqual("33", firstRecord.line);
Assert.AreEqual("444444", firstRecord.pid);
var secondRecord = records[1];
Assert.AreEqual("11", secondRecord.proj_name);
Assert.AreEqual("22", secondRecord.iso);
Assert.AreEqual("33", secondRecord.line);
Assert.AreEqual("4", secondRecord.pid);
Console.Read();
}
}