473,385 Members | 1,769 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.

Reading Fixed Length ASCII/Text File

I have the following code to read the following file (very simple).
I'm trying to extract only the date, but with this code I'm extracting
the entire record. What am I doing wrong? What am I missing?

Ken

c:\temp\namedate.txt = Ken Hall08/27/56 (One record)

Module Module1

Public Structure Record1
<VBFixedString(8)> Public name As String
<VBFixedString(8)> Public date1 As String
End Structure
Sub Main()
Try
Dim line As String
Dim Record2 As New Record1

' Create an instance of StreamReader to read from a file.
Dim sr As System.IO.StreamReader = New
System.IO.StreamReader("c:\temp\namedate.txt")
' Read and display the lines from the file until the end
' of the file is reached.
Do
line = sr.ReadLine()
Record2.date1 = line
Console.WriteLine(Record2.date1)
Loop Until line Is Nothing
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
End Sub

End Module
Nov 20 '05 #1
3 1747
"Ken Hall" <ke*@enormtech.com> wrote...
I have the following code to read the following file (very simple).
I'm trying to extract only the date, but with this code I'm extracting
the entire record. What am I doing wrong? What am I missing? Record2.date1 = line


I think the "error" is this line where you intend to assign the value of the
line to Record2 not the date1 member of that structure. However... (there
is always a however) you can't assign a string to a structure of Record1
type. So that's not going to work either.

How about something like this (I named it RecStruct rather than Record1:

Public Structure RecStruct

Public data As String

Public ReadOnly Property name() As String
Get
name = data.Substring(0, 8)
End Get
End Property

Public ReadOnly Property date1() As String
Get
date1 = data.Substring(8, 8)
End Get
End Property

End Structure
Now you can read directly into it:

Rec.data = sr.ReadLine()

and output the properties:

Debug.WriteLine(Rec.name)
Debug.WriteLine(Rec.date1)

you'll have to adjust your do... loop until a bit also as it will attempt to
output the name and date1 when rec.data is nothing unless you skip over
those lines

Tom

Nov 20 '05 #2
* "Tom Leylan" <ge*@iamtiredofspam.com> scripsit:
"Ken Hall" <ke*@enormtech.com> wrote...
I have the following code to read the following file (very simple).
I'm trying to extract only the date, but with this code I'm extracting
the entire record. What am I doing wrong? What am I missing?

Record2.date1 = line


I think the "error" is this line where you intend to assign the value of the
line to Record2 not the date1 member of that structure. However... (there
is always a however) you can't assign a string to a structure of Record1
type. So that's not going to work either.


Really nice solution.

:-)

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3
Ken
Thanks, Tom. Very helpful.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #4

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

Similar topics

14
by: Job Lot | last post by:
I have tab delimited text file which gets populated on daily basis via automated process. New entry is written at the bottom. I need to create a utility which makes a copy of this file with 10 most...
7
by: jamait | last post by:
Hi all, I m trying to read in a text file into a datatable... Not sure on how to split up the information though, regex or substrings...? sample: Col1 Col2 ...
40
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
6
by: Adrian Lin | last post by:
Is there a way to read a specfic line in a text file? For instance if I wanted to just read the 5th line is that possible without having to step through the other lines? Essentially I am doing huge...
13
by: Martin Herbert Dietze | last post by:
Hi, I need to calculate the physical length of text in a text input. The term "physical" means in this context, that I consider 7bit-Ascii as one-byte-per character. Other characters may be...
18
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
0
by: tshad | last post by:
I can't seem to retrieve messages that are not in my mailbox from Exchange. If I am reading mail from my Exchange server, I will get messages that are in my inbox that have already been read but...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
8
by: Peter Bradley | last post by:
Hi, I wonder if anyone can help me out? I'm trying to implement an EPP (rfc4934 and rfc4930) client. So far I've managed to connect and authorise using an X509 Certificate. This should...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...

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.