473,385 Members | 1,730 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,385 software developers and data experts.

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 9110
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/cpconReadingTextFromFile.asp

String.Substring Method
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemstringclasssubstringtopic.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.microsoft.com> wrote in message
news:2Q*************@cpmsftngxa10.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/cpconReadingTextFromFile.asp

String.Substring Method
http://msdn.microsoft.com/library/de...us/cpref/html/ frlrfsystemstringclasssubstringtopic.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**************@tk2msftngp13.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.microsoft.com> wrote in message
news:2Q*************@cpmsftngxa10.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/cpconReadingTextFromFile.asp

String.Substring Method

http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemstringclasssubstringtopic.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.ReadLine Method
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfSystemIOStreamReaderClassReadLineTopic.asp
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.WriteLine(line)
End While
sr.Close()
Catch E As Exception
' Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(E.Message)
End Try

StreamReader.Read Method (Char[], Int32, Int32)
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemiostreamreaderclassreadtopic2.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.microsoft.com> wrote in message
news:AE**************@cpmsftngxa10.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.ReadLine Method
http://msdn.microsoft.com/library/de...us/cpref/html/ frlrfSystemIOStreamReaderClassReadLineTopic.asp
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.WriteLine(line)
End While
sr.Close()
Catch E As Exception
' Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(E.Message)
End Try

StreamReader.Read Method (Char[], Int32, Int32)
http://msdn.microsoft.com/library/de...us/cpref/html/ frlrfsystemiostreamreaderclassreadtopic2.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
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 " ...
1
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...
1
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...
8
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...
7
by: nizar.jouini | last post by:
Hello. I have long text file whitch is formatted like this: nextrow4 asdf asdf
3
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...
5
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...
12
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.