importing a fixed field length formatted text file into a dataset

  • Thread starter Thread starter Neil Robbins
  • Start date Start date
N

Neil Robbins

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.
 
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/default.asp?url=/library/en-us/cpguide/htm
l/cpconReadingTextFromFile.asp

String.Substring Method
http://msdn.microsoft.com/library/default.asp?url=/library/en-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.
 
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.
 
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.
 
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/default.asp?url=/library/en-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/default.asp?url=/library/en-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.
 
Hi Peter,

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

Many thanks,

Neil R.
 
Back
Top