By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,186 Members | 1,530 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,186 IT Pros & Developers. It's quick & easy.

c# fixed length file processing

P: n/a
Is there any easy way to read into a dataset a file that was created
from a BCP command. The file is in a fixed length format. I would
also like to be able to write out a dataset to the same type of file
structure.
The obvious solution would be to use XML file processing however that
is not an option as I have a third party vendor that is not capable of
processing XML files.
Thanks in advance

Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ecov,

I think that the easiest way to do this at first would be to create a
structure which has fields that correspond to the different columns in the
fixed file format. The types of these fields would have the attributes on
them for marshaling to unmanaged code (it would have all the fixed length
strings declared properly as well).

Then, what you do is allocate a block of unmanaged memory the length of
one of your records. You call ReadFile (through P/Invoke), passing the
length of one record, and the address of this block of unmanaged memory.

You can then call Marshal.PtrToStructure to convert the structure into
managed code. If your record is composed of all blittable types (read, no
strings), then you can use unsafe code to make it even faster, to eliminate
the marshaling overhead.

Once you have the structure, you will have to put it in a dataset
yourself. You can probably name your structure's fields so that you can use
reflection to copy the rows over.

Finally, you would have to reverse the process to write it all back.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ecov" <e_*****@hotmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Is there any easy way to read into a dataset a file that was created
from a BCP command. The file is in a fixed length format. I would
also like to be able to write out a dataset to the same type of file
structure.
The obvious solution would be to use XML file processing however that
is not an option as I have a third party vendor that is not capable of
processing XML files.
Thanks in advance

Nov 17 '05 #2

P: n/a
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eQ*************@TK2MSFTNGP09.phx.gbl...
ecov,

I think that the easiest way to do this at first would be to create a
structure which has fields that correspond to the different columns in the
fixed file format. The types of these fields would have the attributes on
them for marshaling to unmanaged code (it would have all the fixed length
strings declared properly as well).

Then, what you do is allocate a block of unmanaged memory the length of
one of your records. You call ReadFile (through P/Invoke), passing the
length of one record, and the address of this block of unmanaged memory.

You can then call Marshal.PtrToStructure to convert the structure into
managed code. If your record is composed of all blittable types (read, no
strings), then you can use unsafe code to make it even faster, to
eliminate the marshaling overhead.

Once you have the structure, you will have to put it in a dataset
yourself. You can probably name your structure's fields so that you can
use reflection to copy the rows over.

Finally, you would have to reverse the process to write it all back.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ecov" <e_*****@hotmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Is there any easy way to read into a dataset a file that was created
from a BCP command. The file is in a fixed length format. I would
also like to be able to write out a dataset to the same type of file
structure.
The obvious solution would be to use XML file processing however that
is not an option as I have a third party vendor that is not capable of
processing XML files.
Thanks in advance


Make sure you apply the StructLayout attribute to the struct

[StructLayout(LayoutKind.Sequential)]
struct MyRecordStructure
{
...
}

otherwise the CLR will decide on the order of fields during interop rather
than what you define in the struct

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Nov 17 '05 #3

P: n/a

"Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
dot uk> wrote in message news:OE**************@TK2MSFTNGP10.phx.gbl...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:eQ*************@TK2MSFTNGP09.phx.gbl...
ecov,

I think that the easiest way to do this at first would be to create a
structure which has fields that correspond to the different columns in
the fixed file format. The types of these fields would have the
attributes on them for marshaling to unmanaged code (it would have all
the fixed length strings declared properly as well).

Then, what you do is allocate a block of unmanaged memory the length
of one of your records. You call ReadFile (through P/Invoke), passing
the length of one record, and the address of this block of unmanaged
memory.

You can then call Marshal.PtrToStructure to convert the structure into
managed code. If your record is composed of all blittable types (read,
no strings), then you can use unsafe code to make it even faster, to
eliminate the marshaling overhead.

Once you have the structure, you will have to put it in a dataset
yourself. You can probably name your structure's fields so that you can
use reflection to copy the rows over.

Finally, you would have to reverse the process to write it all back.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ecov" <e_*****@hotmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Is there any easy way to read into a dataset a file that was created
from a BCP command. The file is in a fixed length format. I would
also like to be able to write out a dataset to the same type of file
structure.
The obvious solution would be to use XML file processing however that
is not an option as I have a third party vendor that is not capable of
processing XML files.
Thanks in advance


Make sure you apply the StructLayout attribute to the struct

[StructLayout(LayoutKind.Sequential)]
struct MyRecordStructure
{
...
}

otherwise the CLR will decide on the order of fields during interop rather
than what you define in the struct

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


Note that this is the default layout for structures in C#, VB.NET and C++.
But IMO it's better to be explicit anyway.
Willy.

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.