473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

c# fixed length file processing

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
3 7637
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
"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Neil Robbins | last post by:
I have a text file that stores a number of records that I need to access in a vb.net application. Each of the fields that make up a record are of a fixed number of bytes. So for instance there is...
2
by: Frank Swarbrick | last post by:
I'm just learning about embedded SQL, so be gentle... My basic question is, if I use a fixed length host variable for a column defined as VARCHAR, will trailing spaces be removed (or not) upon...
1
by: Rick Knospler | last post by:
I am trying to convert a vb6 project to vb.net. The conversion worked for the most part except for the fixed length strings and fixed length string arrays. Bascially the vb6 programmer stored all...
13
by: Neil Cerutti | last post by:
Many of the file formats I have to work with are so-called fixed-format records, where every line in the file is a record, and every field in a record takes up a specific amount of space. For...
2
by: spacix | last post by:
Does anyone know a work around for "table-layout: fixed;" to prevent the automatic evenly space cells width without assigning classes or ID to cells? My program prints a HTML "report" file and I...
1
by: kendrick82 | last post by:
Hi, I would like to seek some advise and assistance regarding the following matter as I am new to VB.Net. I'll appreciate any helps render. I am developing a VB application using VB.Net 2003 to...
6
by: ssharpjr | last post by:
Hi Guys, I'm new to Python (mostly) and I'm wanting to use it for a new project I'm faced with. I have a machine (PLC) that is dumping its test results into a fixed- length text file. I need...
2
by: Edwin.Madari | last post by:
#your thought is right. ======================================================= def sizes2fields(sizes): d = begin = 0 for i in sizes: if begin: end = begin + i else: end = i...
8
by: iheartvba | last post by:
Hi I am using Access 2007 and am trying to export a query to a fixed length text file. I tried using the following code to export the text file: DoCmd.TransferText acExportFixed, , "qryFFRDeFile",...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.