473,396 Members | 2,021 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,396 software developers and data experts.

How to read text file line by line?

Hi,

I want read line by line and characters. The characters are fix length
text file, no specific delimited method between each fields. The first
line is header line, the last line is footer. Between the first line
and last line is the content, and I will have to load the content to
database.

How to read line by line? I use For lineNum = LBound(lines) To
(UBound(lines) - 1) which generate error because it does read the
array.
Can someone show me how?

Thx

sample file:
------------------------------------------

H2004/07/2709:52:484.0TEST123MARKETING800302096510
D009084826009084826397000763333WITOWSKIDICK 41000620000520000010
F000W20040727FR3970007122228000000124444NWITOWSKID ICK 20040727250USD
D.........
F.........
T11 0 0 11 0 4250 0 0

my code:
-------------------------------------------------
Public Function ProcessRET(ByRef strFilepath As String)
Dim EpayConnection As New SqlConnection
Dim cmdBatchName As New SqlCommand
Dim dtrBatchName As SqlDataReader
EpayConnection.ConnectionString = strEpayDBConn
With cmdBatchName
.Connection = EpayConnection
.CommandText = "SELECT File_Name FROM EPay_Batch_Table Where
File_Status=2"
End With
EpayConnection.Open()
dtrBatchName = cmdBatchName.ExecuteReader()
dtrBatchName.Read()
Dim strRetFileName As String
strRetFileName = dtrBatchName("File_Name")
Dim ReturnFileName As String = strRetFileName.Replace(".in", ".RET")
Dim flRetBatch As File
Dim stRetBatch As FileStream
Dim smRetBatch As StreamReader
Try
Dim sr As New StreamReader(strFilepath &
ReturnFileName)
'Open the text file into a stream reader
Dim sRetLine As String
Dim sBatchNum As String
Dim sRecordType As String
Dim sEmpID As String
Dim sSSN As String
Dim sCardNum As Int32
Dim sErrorCode As String
Try
Dim lines() As String
Dim contents As String
Dim lineNum As Integer
sRetLine = sr.ReadLine
lines = Split(contents, vbCrLf)
If Left$(sRetLine, 1) = "H" Then
sBatchNum = (Mid$(sRetLine, 73, 13))
Dim BatchNum As Int32
BatchNum = Convert.ToInt32(sBatchNum)
End If
sRetLine = sr.ReadLine
For lineNum = LBound(lines) To (UBound(lines) - 1)
'sRetLine = sr.ReadLine
If Left$(sRetLine, 1) = "D" Or "R" Then
sBatchNum = RTrim(Mid$(sRetLine, 73, 85))
End If
Next lineNum
Catch err As Exception
MsgBox("File retrieve fail", err.Message)
End
End Try
Catch
End Try
End Function

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Nov 21 '05 #1
3 5193
This is the idea. Code not tested, but it should work. It will write out
every line to the output window.

try
Dim lineNum As Integer
sRetLine = sr.ReadLine
If Left$(sRetLine, 1) = "H" Then
sBatchNum = (Mid$(sRetLine, 73, 13))
Dim BatchNum As Int32
BatchNum = Convert.ToInt32(sBatchNum)
End If
Do While not sRetLine is nothing
Debug.WriteLine(sr)
sRetLine = sr.ReadLine
Loop
Catch

Chris

"JenHu" <je**********@hotmail-dot-com.no-spam.invalid> wrote in message
news:41**********@Usenet.com...
Hi,

I want read line by line and characters. The characters are fix length
text file, no specific delimited method between each fields. The first
line is header line, the last line is footer. Between the first line
and last line is the content, and I will have to load the content to
database.

How to read line by line? I use For lineNum = LBound(lines) To
(UBound(lines) - 1) which generate error because it does read the
array.
Can someone show me how?

Thx

sample file:
------------------------------------------

H2004/07/2709:52:484.0TEST123MARKETING800302096510
D009084826009084826397000763333WITOWSKIDICK 41000620000520000010
F000W20040727FR3970007122228000000124444NWITOWSKID ICK 20040727250USD
D.........
F.........
T11 0 0 11 0 4250 0 0

my code:
-------------------------------------------------
Public Function ProcessRET(ByRef strFilepath As String)
Dim EpayConnection As New SqlConnection
Dim cmdBatchName As New SqlCommand
Dim dtrBatchName As SqlDataReader
EpayConnection.ConnectionString = strEpayDBConn
With cmdBatchName
Connection = EpayConnection
CommandText = "SELECT File_Name FROM EPay_Batch_Table Where
File_Status=2"
End With
EpayConnection.Open()
dtrBatchName = cmdBatchName.ExecuteReader()
dtrBatchName.Read()
Dim strRetFileName As String
strRetFileName = dtrBatchName("File_Name")
Dim ReturnFileName As String = strRetFileName.Replace(".in", ".RET")
Dim flRetBatch As File
Dim stRetBatch As FileStream
Dim smRetBatch As StreamReader
Try
Dim sr As New StreamReader(strFilepath &
ReturnFileName)
'Open the text file into a stream reader
Dim sRetLine As String
Dim sBatchNum As String
Dim sRecordType As String
Dim sEmpID As String
Dim sSSN As String
Dim sCardNum As Int32
Dim sErrorCode As String
Try
Dim lines() As String
Dim contents As String
Dim lineNum As Integer
sRetLine = sr.ReadLine
lines = Split(contents, vbCrLf)
If Left$(sRetLine, 1) = "H" Then
sBatchNum = (Mid$(sRetLine, 73, 13))
Dim BatchNum As Int32
BatchNum = Convert.ToInt32(sBatchNum)
End If
sRetLine = sr.ReadLine
For lineNum = LBound(lines) To (UBound(lines) - 1)
'sRetLine = sr.ReadLine
If Left$(sRetLine, 1) = "D" Or "R" Then
sBatchNum = RTrim(Mid$(sRetLine, 73, 85))
End If
Next lineNum
Catch err As Exception
MsgBox("File retrieve fail", err.Message)
End
End Try
Catch
End Try
End Function

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*

Nov 21 '05 #2
"JenHu" <je**********@hotmail-dot-com.no-spam.invalid> schrieb:
I want read line by line and characters. The characters are fix length
text file, no specific delimited method between each fields. The first
line is header line, the last line is footer. Between the first line
and last line is the content, and I will have to load the content to
database.


Reading a text file line-by-line or blockwise with a progress indicator
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=readfile&lang=en>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #3
> I want read line by line and characters. The characters are fix length
text file, no specific delimited method between each fields. The first
line is header line, the last line is footer. Between the first line
and last line is the content, and I will have to load the content to
database.


Both of the other suggestions are pretty goo, but I'd through in an
alternative. You mention that you're loading this into a database. If it
happens to be a SQL Server, you could investigate the BULK INSERT T-SQL
statement. It does *blistering fast* imports.

--
Peace & happy computing,

Mike Labosh, MCSD
"I have no choice but to believe in free will."
Nov 21 '05 #4

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

Similar topics

1
by: David Thomas | last post by:
Hi there, a while ago, I posted a question regarding reading japanese text from a text file. Well, since I solved the problem, I thought I'd post my solution for the benefit of other people with...
6
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a...
3
by: John Flynn | last post by:
hi, having problems reading from and writing back to the same file. basically, i want to read lines of text from a file and reverse them and write them back to the same file.. it has to...
4
by: yo_mismo | last post by:
Hi everyone, I'm trying to read the first line of a file this way: .... .... .... .... new_line=0; while((read=read(fd, &info, sizeof(info))) > 0 && !new_line){ if (strcmp(&info, "\n") !=...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
7
by: bowlderster | last post by:
Hello, all. This is the text file named test.txt. 1041 1467 7334 9500 2169 7724 3478 3358 9962 7464 6705 2145 6281 8827 1961 1491 3995 3942 5827 6436 6391 6604 4902 1153 1292 4382 9421 1716...
6
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
4
by: Keith G Hicks | last post by:
I'm trying to read a text file and alter the contents of specific lines in the file. I know how to use streamreader to read each line of a file. I'm doing that already to get the data into a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
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,...

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.