From: Magus on
On Mar 12, 2:10 pm, "Mike Lovell" <dont.re...(a)gotinker.com> wrote:
> > Here is the code that saves to array, then to file.
>
> > procedure TMainForm.SaveJobsToArray;
> > var
> > i,jobcount: Integer;
> > begin
> > //loop through all the Listview items and assign them to the array
> >  jobcount := ExtListView1.Items.Count;
> >  SetLength(Jobs,jobcount);
> >  for i := 0 to jobcount-1 do
> >    begin
> >      with ExtListView1.Items[i] do begin
> >        Jobs[i].ReceiveDate := Caption;
> >        Jobs[i].JobName := SubItems[0];
> >        Jobs[i].Contractor := SubItems[1];
> >        Jobs[i].AssignDate := SubItems[2];
> >        Jobs[i].DetailerName := SubItems[3];
> >      end;//with
> >    end;
> > end;
>
> > procedure TMainForm.SaveJobToFile(filepath: string);
> > var
> >  MyJobFile: TRebarJobFile; //file used to store rebar jobs
> > //  Job: TRebarJob; //structure of file information
> >  i: integer; //basic loop counters
> > begin
> >  SaveJobsToArray;
> >  try
> >  System.Assign(MyJobFile,filepath);
> >  Rewrite(MyJobFile);
> >  for i := 0 to Length(Jobs)-1 do
> >    begin
> >      Write(MyJobFile,Jobs[i]);
> >    end;
> >  finally
> >    System.CloseFile(MyJobFile);
> >    end;
> > end;
>
> > I did find another article that is almost what I'm needing:
> >http://groups.google.com/group/microsoft.public.dotnet.languages.csha...
>
> > The problem is that the code doesn't work. Maybe because I'm using a
> > newer version of C#....
>
> If all the arrays are stored in the same output file, could you post an
> example of the file with a few entries in it?  It's a two minute coding job
> to write something to do this for you.
>
> --
> Mike
> GoTinker, C# Bloghttp://www.gotinker.com

Here is the example:
12/28/2009MINOR LANE PUMPING
STATIONS
K C Ctt
Const.
01/05/2010Ron
12/28/2009(ST.JOSEPH CATHOLIC CHURCH RESTROOM ADD'N'S
CROSSROADS,NC
Sullivan &
Cozart
12/29/2009Dony

There are a lot of spaces and characters. Like on the last entry. The
user is "Don" and not "Dony".
From: Mike Lovell on
> Here is the example:
> 12/28/2009MINOR LANE PUMPING
> STATIONS
> K C Ctt
> Const.
> 01/05/2010Ron
> 12/28/2009(ST.JOSEPH CATHOLIC CHURCH RESTROOM ADD'N'S
> CROSSROADS,NC
> Sullivan &
> Cozart
> 12/29/2009Dony
>
> There are a lot of spaces and characters. Like on the last entry. The
> user is "Don" and not "Dony".

Hrm, well that could be reading it with the wrong encoding. But then
there's going to be a certain binary structure to the file by the looks of
it.

I think the exact formatting is going to be lost in the news post/reader.

Could you do this, make a file like above with test data using delphi (3 or
4 records), then from C#:

Console.WriteLine(Convert.ToBase64String(File.ReadAllBytes("filename.ext")));

And post back the output. That will survive being sent as plaint text

--
Mike
GoTinker, C# Blog
http://www.gotinker.com

From: Magus on
From: Mike Lovell on
> Ok, here is the output:
>
> <snip>

BTW/ if I'm right on this you owe me a "telling everyone you know about my
blog" !!! :op


Your input data?


receiveDate: 03/12/2010
jobName: BJK
contractor: David Construction
assignDate: 03/12/2010
detailerName: Ron

receiveDate: 03/11/2010
jobName: MUTC Cave
contractor: AML
assignDate: 03/11/2010
detailerName: Ron

receiveDate: 03/11/2010
jobName: Steindam Apartments - Fort Knox
contractor: Roederer Construction
assignDate: 03/11/2010
detailerName: Ron

receiveDate: 03/10/2010
jobName: Camp Piomingo
contractor: David Construction
assignDate: 03/10/2010
detailerName: Ron

receiveDate: 03/10/2010
jobName: Caldwell E-6953 Independence Kansas
contractor: Caldwell
assignDate: 03/11/2010
detailerName: Ron

receiveDate: 03/08/2010
jobName: Dollar General English, In
contractor: Sprigler
assignDate: 03/09/2010
detailerName: Ron

receiveDate: 03/05/2010
jobName: Glassworks Garage
contractor: Bosse-Mattingly
assignDate: 03/11/2010
detailerName: Ron

receiveDate: 03/05/2010
jobName: Clay Commons
contractor: Bosse-Mattingly
assignDate: 03/11/2010
detailerName: Ron

receiveDate: 03/04/2010
jobName: Norton Cancer
contractor: Sullivan & Cozart
assignDate: 03/04/2010
detailerName: John

receiveDate: 03/03/2010
jobName: E.W. Brown LG&E
contractor: Kelsey
assignDate: 03/03/2010
detailerName: Ron

receiveDate: 03/02/2010
jobName: Scottsburg TIE Center
contractor: Broughton
assignDate: 03/10/2010
detailerName: John

receiveDate: 03/02/2010
jobName: Ft. Knox
contractor: Kelsey
assignDate: 03/04/2010
detailerName: Ron

receiveDate: 03/01/2010
jobName: Taffel
contractor: E&W
assignDate: 03/01/2010
detailerName: Ron

receiveDate: 03/01/2010
jobName: Wendy's Clarksville, Tn
contractor: Castlewood
assignDate: 03/01/2010
detailerName: Ron

receiveDate: 02/26/2010
jobName: Mammoth Cave
contractor: Howard Pence
assignDate: 03/01/2010
detailerName: Ron

receiveDate: 02/24/2010
jobName: Kohl's
contractor: AML
assignDate: 02/24/2010
detailerName: Ron / John

receiveDate: 02/22/2010
jobName: GENENTECH
contractor: Parco
assignDate: 02/23/2010
detailerName: Ron

receiveDate: 02/19/2010
jobName: E.W.BROWN GYPSUM-MERCER CO.
contractor: Evans Const/Kelsey Const
assignDate: 02/19/2010
detailerName: Ron

receiveDate: 02/17/2010
jobName: CALDWELL TANK 1,000,000 GAL.FDN E-6984 ELKHART,IND
contractor: Caldwell Tanks
assignDate: 02/19/2010
detailerName: Don

receiveDate: 02/16/2010
jobName: DECO-NEW PRESS
contractor: Sullivan & Cozart
assignDate: 02/17/2010
detailerName: Don

receiveDate: 02/08/2010
jobName: CALDWELL TANK 750,000 GAL DOME E-6950 SANGAMON CO.,ILL
contractor: Caldwell Tanks
assignDate: 02/10/2010
detailerName: Don

receiveDate: 01/26/2010
jobName: KY. TRAILER
contractor: Parco
assignDate: 01/26/2010
detailerName: Don

receiveDate: 01/22/2010
jobName: FIELD ELEM.SCHOOL-MECH.VAULT REPAIR
contractor: David Const.
assignDate: 01/25/2010
detailerName: Don

receiveDate: 01/13/2010
jobName: PIOMINGO POOL HOUSE
contractor: David Const.
assignDate: 01/14/2010
detailerName: Ron

receiveDate: 01/11/2010
jobName: KENTUCKY EXPO HORSE BARNS
contractor: Parco
assignDate: 01/11/2010
detailerName: Don

receiveDate: 01/11/2010
jobName: WESTPORT RD. CHURCH of CHRIT
contractor: A M L Construction
assignDate: 01/19/2010
detailerName: Ron

receiveDate: 01/08/2010
jobName: ESSROC MISC.-SELLERSBURG
contractor: Huelsman-Sweeney Const.
assignDate: 01/08/2010
detailerName: Don

receiveDate: 01/05/2010
jobName: CALDWELL TANK 1,000,000 GAL,DOME E-6948 ADAIR CO.,KY.
contractor: Caldwell Tanks
assignDate: 01/06/2010
detailerName: Don


If so, the format was, first byte is the length of the data ... Then you
read the maximum field length (from your first post). Then you convert to
ASCII the result of that (but only convert the length of the data, given by
the first byte) ... Then repeat...

!!!DISCLAIMER!!!
This code is a 5 minute hack up, it's not my best but it demonstrates what I
did...


static void Main(string[] args)
{
var base64 = "<base64 data you posted>";
var rawData = Convert.FromBase64String(base64);
var stream = new MemoryStream(rawData);

while (true)
{
var receiveDate = GetBlock(stream, 10);
var jobName = GetBlock(stream, 150);
var contractor = GetBlock(stream, 100);
var assignDate = GetBlock(stream, 10);
var detailerName = GetBlock(stream, 30);

if (detailerName == null) break;

Console.WriteLine("receiveDate: {0}", receiveDate);
Console.WriteLine("jobName: {0}", jobName);
Console.WriteLine("contractor: {0}", contractor);
Console.WriteLine("assignDate: {0}", assignDate);
Console.WriteLine("detailerName: {0}", detailerName);

Console.WriteLine();
}

Console.ReadLine();
}


private static string GetBlock(Stream stream, int maxSize)
{
var buffer = new byte[maxSize];

stream.Read(buffer, 0, 1); // first byte is the size

var size = buffer[0];

var length = stream.Read(buffer, 0, maxSize); // read the entire field
length

if (length == 0) return null; // nothing returned

return Encoding.ASCII.GetString(buffer, 0, size); // return the ASCII
string
}



--
Mike
GoTinker, C# Blog
http://www.gotinker.com


From: Arne Vajhøj on
On 12-03-2010 13:24, Magus wrote:
> On Mar 12, 1:17 pm, Willem van Rumpt<noth...(a)nowhere.com> wrote:
>> Magus wrote:
>>> There is an old program that I'm rewriting. The program stores the
>>> data into an array and dumps it to a file. I can't seem to properly
>>> load the file though.
>>
>>> Here is the record I'm working with:
>>
>>> type
>>> TMyJob = record
>>> ReceiveDate: string[10];
>>> JobName: string[150];
>>> Contractor: string[100];
>>> AssignDate: string[10];
>>> DetailerName: string[30];
>>> end;
>>
>>> Jobs: array of TMyJob;
>>
>>> Does anyone know how to convert this?
>>
>> That would depend how the contents are streamed to file. Language aside,
>> if the "dumping" is done by some custom streamer, it's impossible to know.
>>
>> It's been a while since I've done any serious work with Delphi (I left
>> at D7), but I think the following questions might be of importance:
>>
>> 1) Is the file defined as a "file" variable (i.e. : var myFile: file of
>> TMyJob (if memory serves)), or is it some custom streaming mechanism?
>>
>> 2) Are the strings pre- or post unicode? (Unicode strings were
>> introduced in what, D2007? D2009?)
>>
>> If you can provide more info about this, I hope I can be of more help.
>> Lacking that, I can think of lots of schemes to read the file, but they
>> all boil down to manual parsing.
>>
>> Also, every once in a while, Rudy Velthuis pops up in here, perhaps he
>> can chip in. You could also raise the question in the embarcadero
>> newsgroups. I don't think the C# Builder newsgroup is very active any
>> more, but maybe they still have an appropriate group to post this
>> question in.
>>
>> --
>> Willem van Rumpt
>
> Here is the code that saves to array, then to file.
>
> procedure TMainForm.SaveJobsToArray;
> var
> i,jobcount: Integer;
> begin
> //loop through all the Listview items and assign them to the array
> jobcount := ExtListView1.Items.Count;
> SetLength(Jobs,jobcount);
> for i := 0 to jobcount-1 do
> begin
> with ExtListView1.Items[i] do begin
> Jobs[i].ReceiveDate := Caption;
> Jobs[i].JobName := SubItems[0];
> Jobs[i].Contractor := SubItems[1];
> Jobs[i].AssignDate := SubItems[2];
> Jobs[i].DetailerName := SubItems[3];
> end;//with
> end;
> end;
>
> procedure TMainForm.SaveJobToFile(filepath: string);
> var
> MyJobFile: TRebarJobFile; //file used to store rebar jobs
> // Job: TRebarJob; //structure of file information
> i: integer; //basic loop counters
> begin
> SaveJobsToArray;
> try
> System.Assign(MyJobFile,filepath);
> Rewrite(MyJobFile);
> for i := 0 to Length(Jobs)-1 do
> begin
> Write(MyJobFile,Jobs[i]);
> end;
> finally
> System.CloseFile(MyJobFile);
> end;
> end;
>
> I did find another article that is almost what I'm needing:
> http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_thread/thread/95d54b93a0627bd3/10af49c6d09a9fd6?hl=en&q=Convert+Delphi+record+structure+to+C+Sharp#10af49c6d09a9fd6
>
> The problem is that the code doesn't work. Maybe because I'm using a
> newer version of C#....

I think the idea is correct, but maybe not the implementation.

The following works here with Delphi 7 and .NET 3.5:

program writerecs;

{$APPTYPE CONSOLE}

uses
SysUtils;

type
TFooBar = record
a : string[10];
b : string[20];
c : string[30];
end;

var
i : integer;
data : array [1..4] of TFooBar;
f : file of TFooBar;

begin
data[1].a := '1a';
data[1].b := '1b';
data[1].c := '1c';
data[2].a := '2a';
data[2].b := '2b';
data[2].c := '2c';
data[3].a := '3a';
data[3].b := '3b';
data[3].c := '3c';
data[4].a := '4a';
data[4].b := '4b';
data[4].c := '4c';
assign(f, 'C:\foobar.dat');
rewrite(f);
for i := low(data) to high(data) do begin
write(f, data[i]);
end;
close(f);
readln;
end.

using System;
using System.IO;
using System.Runtime.InteropServices;

namespace E
{
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi,Pack=1)]
public struct TFooBar
{
private byte alen;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=10)]
public string a;
private byte blen;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=20)]
public string b;
private byte clen;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=30)]
public string c;
}
public class Program
{
public static void Main(string[] args)
{
Stream stm = new FileStream(@"C:\foobar.dat",
FileMode.Open, FileAccess.Read);
TFooBar[] data = new TFooBar[4];
int bufsiz = Marshal.SizeOf(typeof(TFooBar));
byte[] b = new byte[bufsiz];
IntPtr buf = Marshal.AllocHGlobal(bufsiz);
for(int i = 0; i < data.Length; i++)
{
stm.Read(b, 0, b.Length);
Marshal.Copy(b, 0, buf, b.Length);
data[i] = (TFooBar)Marshal.PtrToStructure(buf,
typeof(TFooBar));
}
Marshal.FreeHGlobal(buf);
stm.Close();
for(int i = 0; i < data.Length; i++)
{
Console.WriteLine(data[i].a + " " + data[i].b + " " +
data[i].c);
}
Console.ReadKey();
}
}
}

Arne