OK I have a binary file that needs to be able to read and then write to from csv data.
I have about 12k records that needs to converted from csv to binary, This hard for me to wrap my head around it personally. Any help would be appreciated. C# with visual studio is what I am using.
This is the file that I am reading
310 KEITH SFGEH6 BRENTWOOD CA 90049 å¤_ š™ÏA 310 KEITH OSAD SDNVCS BRENTWOOD CA 90049 ¤_ B 310 57I M4 8M4M8 8M BRENTWOOD CA 90049 ?¤_ 8A 310 WERTHWR SFGHERH BRENTWOOD CA 90049 À¤_ B 310 HMDFGH ADGNBSFDGNFS BRENTWOOD CA 90049 ö¤_ LB 310 GHMNDGMH SFMSFGHMNS BRENTWOOD CA 90049 ¤_ 8B 310 FGSXFDGH SDGHSDHG BRENTWOOD CA 90049 þ$¤_ ˜A 310 HTAH AFGHBADF BRENTWOOD CA 90049 &¤_ ÈA 310 ADHAD ADGH BRENTWOOD CA 90049 ©)¤_ |B 310 TYDJU DGTJ BRENTWOOD CA 90049 &*¤_ °B 310 E57IE5 ETUI BRENTWOOD CA 90049 J,¤_ ÍÌ\@ 207 TOP ME 04011
and this is the structure I am told. Each record has the following structure: #define DELIVERY_MAX_HISTORY 6
#pragma pack(push,1)
typedef struct
{
char area_code[4];
char phone[8];
char name[40];
char address1[40];
char address2[40];
char city[40];
char state[3];
char zip[13];
char directions1[40];
char directions2[40];
char comment1[40];
char comment2[40];
unsigned long last_order_dates[DELIVERY_MAX_HISTORY];
float last_order_totals[DELIVERY_MAX_HISTORY];
short last_order_check_num[DELIVERY_MAX_HISTORY];
char has_bad_checks;
char ext[10]; // Currently only 4 bytes are allowed
char Zone[10]; // not unused
char extra[31];
} delivery_data_type;
#pragma pack(pop)
OK so have put this together but I have no data output to the.bin file. it just sits and run and runs
class Program
{
static void Main(string[] args)
{
using(var reader = new StreamReader(@"c:\test\custdel.csv"))
Console.WriteLine("DELIVERY Converter");
var serializer = new BinarySerializer();
using var csv = new CsvReader(Console.In, CultureInfo.InvariantCulture);
var persons = csv.GetRecords<Person>();
using var file = new FileStream("out.bin", FileMode.Create, FileAccess.Write);
foreach (var person in persons)
{
serializer.Serialize(file, person);
}
}
public class Person
{
[FieldOrder(0)] // from BinarySerialization
public string areacode { get; set; }
[FieldOrder(1)]
public string phone { get; set; }
[FieldOrder(2)]
public int name { get; set; }
[FieldOrder(3)]
public string ddress1 { get; set; }
[FieldOrder(4)]
public int address2 { get; set; }
[FieldOrder(5)]
public string city { get; set; }
[FieldOrder(6)]
public int state { get; set; }
[FieldOrder(7)]
public string zip { get; set; }
[FieldOrder(8)]
public int directions1 { get; set; }
[FieldOrder(9)]
public string directions2 { get; set; }
[FieldOrder(10)]
public int comment1 { get; set; }
[FieldOrder(11)]
public string comment2 { get; set; }
[FieldOrder(12)]
public int has_bad_checks { get; set; }
[FieldOrder(13)]
public string ext { get; set; }
[FieldOrder(14)]
public int Zone { get; set; }
[FieldOrder(15)]
public string extra { get; set; }
}
}
and this is the csv file
FieldOrder(0),FieldOrder(1),FieldOrder(2),FieldOrder(3),FieldOrder(4),FieldOrder(5),FieldOrder(6),FieldOrder(7),FieldOrder(8),FieldOrder(9),FieldOrder(10),FieldOrder(11),FieldOrder(12),FieldOrder(13),FieldOrder(14),FieldOrder(15)
207,207-210-2127,ADRIAN,160 COLUMBIA AVE,,BRUNSWICK,ME,4011,,,,,,,, 207,446-1989,ANNA,16 BRUNSWICK STATION,,BRUNSWICK,ME,,secondfloor,,,,,,,
ok I have found that problem and resolved and on to the next one. Getting an error when debugging at the "foreach (var person in person)" and this is what I am getting for the error CsvHelper.TypeConversion.TypeConverterException: 'The conversion cannot be performed. Text: 'name' MemberType: System.Int32 TypeConverter: 'CsvHelper.TypeConversion.Int32Converter' IReader state: ColumnCount: 0 CurrentIndex: 2 HeaderRecord:
and my code change also and I removed the header from the csv file as I was getting another error on the headers:
{
Console.WriteLine("DELIVERY Converter");
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false,
};
using (var reader = new StreamReader(@"c:\test\custdel.csv"))
using (var csv = new CsvReader(reader, config))
{
var persons = csv.GetRecords<Person>();
var serializer = new BinarySerializer();
using var file = new FileStream("out.bin", FileMode.Create, FileAccess.Write);
foreach (var person in persons)
{
serializer.Serialize(file, person);
}
}
}
You could try something like
using BinarySerialization;
using CsvHelper;
...
public class Person
{
[FieldOrder(0)] // from BinarySerialization
public string FirstName { get; set; }
[FieldOrder(1)]
public string LastName { get; set; }
[FieldOrder(2)]
public int ID { get; set; }
}
private static void Main()
{
var serializer = new BinarySerializer();
using var csv = new CsvReader(Console.In, CultureInfo.InvariantCulture);
var persons = csv.GetRecords<Person>();
using var file = new FileStream("out.bin", FileMode.Create, FileAccess.Write);
foreach (var person in persons)
{
serializer.Serialize(file, person);
}
}
It reads cvs from standard input and write binary representation to out.bin
See https://github.com/jefffhaynes/BinarySerializer and https://joshclose.github.io/CsvHelper/ for details
Example of file that you reading looks not like csv, so maybe you should consider something like https://github.com/mikeacjones/FixedWidthFileUtils to read file
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.