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

importing a fixed field length formatted text file into a dataset

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.