473,654 Members | 3,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7658
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.PtrToSt ructure 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.co m

"ecov" <e_*****@hotmai l.com> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.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.c om> wrote in
message news:eQ******** *****@TK2MSFTNG P09.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.PtrToSt ructure 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.co m

"ecov" <e_*****@hotmai l.com> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.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(La youtKind.Sequen tial)]
struct MyRecordStructu re
{
...
}

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******** ******@TK2MSFTN GP10.phx.gbl...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:eQ******** *****@TK2MSFTNG P09.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.PtrToSt ructure 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.co m

"ecov" <e_*****@hotmai l.com> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.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(La youtKind.Sequen tial)]
struct MyRecordStructu re
{
...
}

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
9123
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 an address field of 240 bytes and there is an id field of 12 bytes. Where the data stored in a field does not fill the available number of bytes then spaces " " are inserted to fill the remaining bytes. There are no delimiters, just fields of a...
2
5607
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 INSERT or UPDATE of this column? I tried it, and it appears they are *not* stripped. However, the Programming Client Applications manual leads me to believe that the spaces should be stripped. A quote from that manual: -------------------------...
1
9412
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 form data in a fixed length structure that is written direct to disk. I need to load the existing files, into a fixed length structure to initialize the form data within the project. I am running into problems with statements like the...
13
3235
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 example, one of my older Python programs contains the following to create a fixed-format text record for a batch of new students: new = file("new.dat", "w") if not new:
2
5870
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 used "table-layout: fixed; width:691px;" because the reports have to be printed on 8.5x11 paper. I didn't want the table to resize or anything. Then after I added that property to the table, the data cells no longer "fit to contents" even with a...
1
3168
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 extract data from text files and import it into datatable. The format of the text file is fixed length/width. Eg. http://img174.imageshack.us/img174/8457/untitledpp4.jpg I am having trouble to do so as I am not sure how to start. I had done...
6
4432
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 to pull this data into a database (MySQL most likely) so that I can access it with Crystal Reports to create daily reports for my engineers.
2
3159
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 d.append((begin, end))
8
18772
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", "C:\qryFFRDeFile.txt", True Access asks for a specification name, now here lies the challenge, how do I create an export specification I have tried the following 2 methods: In access 2007: Step1: I right clicked the query and chose export to...
0
8372
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8814
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8706
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8591
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2709
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1592
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.