473,789 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

importing a fixed field length formatted text file into a dataset

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 fixed
length.

The data held is non relational.
The data will never be edited.
There will never be more than one user accessing it at any one time.

I need to load the data held in this file into my application so that it can
be easily viewed by a user.

I had thought to do this through the use of a dataset to hold the data and a
DataGrid to view it.

Could anyone suggest an efficient way of loading this data into a dataset?

Any help would be gratefully recieved.

Neil R.
Nov 20 '05 #1
5 9140
Hi Neil,

Since there is no delimiler, I think we have to parse the text file
manually.
We can use the StreamReader to read the data from the file line by line and
use the substring method of string to retrieve the fix length of text from
the line of string.

Reading Text from a File
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconReadingTex tFromFile.asp

String.Substrin g Method
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemstri ngclasssubstrin gtopic.asp

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #2
Hi Peter,

Thanks for the advise, I'm going to try what you suggest. What had stopped
me from trying this before is that I was worried that the whole file might
be taken to constitute a single line. Perhaps you could tell me how the
system recognises a line has finished. Is a line of a fixed length? Is it
delimited by something (vbCr perhaps)? I would be very interested to find
out and I suspect it might be useful for me to know.

Many thanks,

Neil R.

""Peter Huang"" <v-******@online.m icrosoft.com> wrote in message
news:2Q******** *****@cpmsftngx a10.phx.gbl...
Hi Neil,

Since there is no delimiler, I think we have to parse the text file
manually.
We can use the StreamReader to read the data from the file line by line and use the substring method of string to retrieve the fix length of text from
the line of string.

Reading Text from a File
http://msdn.microsoft.com/library/de...us/cpguide/htm l/cpconReadingTex tFromFile.asp

String.Substrin g Method
http://msdn.microsoft.com/library/de...us/cpref/html/ frlrfsystemstri ngclasssubstrin gtopic.asp

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #3
Hello again Peter,

I've just used this piece of code and it does bring in the whole file as a
single line - so far without that causing a problem. In fact having checked
the maximum length of a string it is very unlikely that this will ever cause
a problem.

If there is a way of reading a fixed number of bytes/characters from a text
file I would be very interested to find out about it.

Thanks for helping me with this,

Kind Regards,

Neil R.

"Neil Robbins" <ne**@talent-centre.com> wrote in message
news:OA******** ******@tk2msftn gp13.phx.gbl...
Hi Peter,

Thanks for the advise, I'm going to try what you suggest. What had stopped
me from trying this before is that I was worried that the whole file might
be taken to constitute a single line. Perhaps you could tell me how the
system recognises a line has finished. Is a line of a fixed length? Is it
delimited by something (vbCr perhaps)? I would be very interested to find
out and I suspect it might be useful for me to know.

Many thanks,

Neil R.

""Peter Huang"" <v-******@online.m icrosoft.com> wrote in message
news:2Q******** *****@cpmsftngx a10.phx.gbl...
Hi Neil,

Since there is no delimiler, I think we have to parse the text file
manually.
We can use the StreamReader to read the data from the file line by line

and
use the substring method of string to retrieve the fix length of text from the line of string.

Reading Text from a File

http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconReadingTex tFromFile.asp

String.Substrin g Method

http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemstri ngclasssubstrin gtopic.asp

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no

rights.


Nov 20 '05 #4
Hi Neil,

Thank you for your quick response.

From the MSDN,
A line is defined as a sequence of characters followed by a line feed
("\n") or a carriage return immediately followed by a line feed ("\r\n").
The string that is returned does not contain the terminating carriage
return or line feed. The returned value is a null reference (Nothing in
Visual Basic) if the end of the input stream is reached.

StreamReader.Re adLine Method
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfSystemIOSt reamReaderClass ReadLineTopic.a sp
If you want to read a fix length of characters from a file we can use
override version of read method of streamreader.
Try
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New StreamReader("c :\edit1.txt")
Dim line As String
' Read and display the lines from the file until the end
' of the file is reached.
Dim buff(14) As Char
Dim rd As Integer = 15
Dim index As Integer = 0
While rd = 15
rd = sr.Read(buff, 0, 15)
line = New String(buff)
Console.WriteLi ne(line)
End While
sr.Close()
Catch E As Exception
' Let the user know what went wrong.
Console.WriteLi ne("The file could not be read:")
Console.WriteLi ne(E.Message)
End Try

StreamReader.Re ad Method (Char[], Int32, Int32)
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemiost reamreaderclass readtopic2.asp
Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #5
Hi Peter,

Thanks a lot for this very helpful - solved my problem..

Many thanks,

Neil R.

""Peter Huang"" <v-******@online.m icrosoft.com> wrote in message
news:AE******** ******@cpmsftng xa10.phx.gbl...
Hi Neil,

Thank you for your quick response.

From the MSDN,
A line is defined as a sequence of characters followed by a line feed
("\n") or a carriage return immediately followed by a line feed ("\r\n").
The string that is returned does not contain the terminating carriage
return or line feed. The returned value is a null reference (Nothing in
Visual Basic) if the end of the input stream is reached.

StreamReader.Re adLine Method
http://msdn.microsoft.com/library/de...us/cpref/html/ frlrfSystemIOSt reamReaderClass ReadLineTopic.a sp
If you want to read a fix length of characters from a file we can use
override version of read method of streamreader.
Try
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New StreamReader("c :\edit1.txt")
Dim line As String
' Read and display the lines from the file until the end
' of the file is reached.
Dim buff(14) As Char
Dim rd As Integer = 15
Dim index As Integer = 0
While rd = 15
rd = sr.Read(buff, 0, 15)
line = New String(buff)
Console.WriteLi ne(line)
End While
sr.Close()
Catch E As Exception
' Let the user know what went wrong.
Console.WriteLi ne("The file could not be read:")
Console.WriteLi ne(E.Message)
End Try

StreamReader.Re ad Method (Char[], Int32, Int32)
http://msdn.microsoft.com/library/de...us/cpref/html/ frlrfsystemiost reamreaderclass readtopic2.asp
Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
9701
by: Adrian Parker | last post by:
I'm using the code below in my project. When I print all of these fixed length string variables, one per line, they strings in questions do not properly pad with 0s. strQuantity prints as " 4". Six spaces than the value of intQuantity. This is correct. But all the others end up being string objects of only 6 characters long (with the exception of strTotal). The left most positions of the string object are being padded with one...
1
4601
by: Thomas R. Hummel | last post by:
Hello, I am importing a file using BCP, with a format file. It is a fixed-width file, which I am importing into a table that has a column for each field in the file. The columns in my import table are all defined as CHAR. I use it as a staging area to check the data (are dates formatted correctly, etc.) before moving it from there. In one of my files I have six records that contain strange characters (ascii codes of 03 and 02 if I am...
1
3680
by: sparks | last post by:
I have never done this and wanted to ask people who have what is the best way. One person said import it to excel, then import it into access table. but since this will be done a lot, I am trying to avoid this extra step. can access read in this file directly into a table as append data? thank you very much for any pointers
8
2552
by: harry | last post by:
Hi Folks: I'm trying to do something that looks simple, but I can't make it work right. It's Access 2000 on a Win2000 computer. I create a database with 5 columbs. The data I need to import is text. The way it's formatted is one line each field, for the 5 fields, then to the next record. The text file looks like this: WPTJ410
7
2143
by: nizar.jouini | last post by:
Hello. I have long text file whitch is formatted like this: nextrow4 asdf asdf
3
7668
by: ecov | last post by:
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.
5
3177
by: hharriel | last post by:
Hi, I am hoping someone can help me with an issue I am having with excel and ms access. I have collected data (which are in individual excel files) from 49 different school districts. All districts have used the same excel template and populated the same 32 data fields (columns). I created one large excel file from all 49 files which gives me a master table of 60,000 or so records. I have tried to import this master table into access...
12
6223
by: JMO | last post by:
I can import a csv file with no problem. I can also add columns to the datagrid upon import. I want to be able to start importing at the 3rd row. This will pick up the headers necessary for the datagrid. Once I can get to that point I need some way to be able to add new data only to the new columns that were added. Here is some of my code: //Function For Importing Data From CSV File public DataSet ConnectCSV(string filetable)
0
9666
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
10408
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
10199
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...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5417
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
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
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
3
2909
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.