469,643 Members | 1,877 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,643 developers. It's quick & easy.

Reading and Writing Text Files

Hey all I have this code that reads from one text file writes to
another unless im on a certian line then it writes the new data however
it only seems to do about 40 lines then quits and I cant figure out why
any help would be much apprechiated

1.
Dim line_num As Integer
2.
Dim filename2 As String
3.
Dim csv_text As String
4.
csv_text = companytxt.Text & "," & Nametxt.Text & "," &
addrtxt.Text & "," & citytxt.Text & "," & statetxt.Text & "," &
ziptxt.Text & "," & countrytxt.Text & "," & phonetxt.Text & "," &
faxtxt.Text & "," & celltxt.Text & "," & titletxt.Text & "," &
assistanttxt.Text & "," & urltxt.Text & "," & altcontacttxt.Text & ","
& altphonetxt.Text & "," & altaddrtxt.Text & "," & altcitytxt.Text &
"," & altstatetxt.Text & "," & altziptxt.Text & "," & territorytxt.Text
& "," & mech1serialnum.Text & "," & mech1vacpump.Text & "," &
mech1spindle.Text & "," & mech1vacpumpserialnum.Text & "," &
mech1filterpartnum.Text & "," & mech2serialnum.Text & "," &
mech2vacpump.Text & "," & mech2spindle.Text & "," &
mech2vacpumpserialnum.Text & "," & mech2filterpartnum.Text & "," &
mech3serialnum.Text & "," & mech3vacpump.Text & "," & mech3spindle.Text
& "," & mech3vacpumpserialnum.Text & "," & mech3filterpartnum.Text &
"," & mech4serialnum.Text & "," & mech4vacpump.Text & "," &
mech4spindle.Text & "," & mech4vacpumpserialnum.Text & "," &
mech4filterpartnum.Text & "," & user1txt.Text & "," & user2txt.Text &
"," & user3txt.Text & "," & user4txt.Text & "," & user5txt.Text & "," &
user6txt.Text & "," & user7txt.Text & "," & user8txt.Text & "," &
user9txt.Text & "," & user10txt.Text & "," & user11txt.Text & "," &
user12txt.Text & "," & user13txt.Text & "," & user14txt.Text & "," &
user15txt.Text
5.
filename2 = "C:\Documents and Settings\Brian Bepristis\My
Documents\Visual Studio Projects\Multicamtemp.csv"
6.
Dim filename As String
7.
filename = "C:\Documents and Settings\Brian Bepristis\My
Documents\Visual Studio Projects\Multicamtemp2.csv"
8.
' Create an empty string array to return to caller.
9.
Dim lines() As String = {}
10.

11.
' Check to see if the file exists.
12.
If File.Exists(filename) Then
13.
' Open a stream reader to get the text from the file.
14.
Dim sr As New StreamReader(filename)
15.
' Read all the file text.
16.
Dim fileText As String = sr.ReadToEnd()
17.
' Close the stream reader
18.
sr.Close()
19.

20.
' Split the text into a string array delimited by
Carriage Return/Line Feed.
21.
lines = Split(fileText, vbCrLf)
22.

23.
' Return
24.
Dim fs As New FileStream(filename2, FileMode.Create)
25.
Dim sw As New StreamWriter(fs)
26.

27.
Dim foundBlank As Boolean
28.
For Each line As String In lines
29.
If line.Length > 0 Then
30.
line_num += 1
31.
If line_num = currecord Then
32.
sw.WriteLine(csv_text)
33.
' Reset blank line flag
34.
foundBlank = False
35.
Else
36.
sw.WriteLine(line)
37.
' Reset blank line flag
38.
foundBlank = False
39.
End If
40.
Else
41.
If Not foundBlank Then
42.
' Blank line: write first one
43.
sw.WriteLine(line)
44.
' Set flag to indicate that blank line
was found
45.
foundBlank = True
46.
End If
47.
End If
48.
Next
49.
sw.Close()
50.
fs.Close()
51.
End If
52.
If System.IO.File.Exists(filename2) = True Then
53.
If System.IO.File.Exists(filename) = True Then
54.
System.IO.File.Delete(filename)
55.
End If
56.
System.IO.File.Copy(filename2, filename)
57.
End If

May 11 '06 #1
3 1940
Are you sure you don't have included a chrw(0) value.

After that you cannot see anything anymore.

I hope this helps,

Cor

"bbepristis" <br*************@gmail.com> schreef in bericht
news:11**********************@v46g2000cwv.googlegr oups.com...
Hey all I have this code that reads from one text file writes to
another unless im on a certian line then it writes the new data however
it only seems to do about 40 lines then quits and I cant figure out why
any help would be much apprechiated

1.
Dim line_num As Integer
2.
Dim filename2 As String
3.
Dim csv_text As String
4.
csv_text = companytxt.Text & "," & Nametxt.Text & "," &
addrtxt.Text & "," & citytxt.Text & "," & statetxt.Text & "," &
ziptxt.Text & "," & countrytxt.Text & "," & phonetxt.Text & "," &
faxtxt.Text & "," & celltxt.Text & "," & titletxt.Text & "," &
assistanttxt.Text & "," & urltxt.Text & "," & altcontacttxt.Text & ","
& altphonetxt.Text & "," & altaddrtxt.Text & "," & altcitytxt.Text &
"," & altstatetxt.Text & "," & altziptxt.Text & "," & territorytxt.Text
& "," & mech1serialnum.Text & "," & mech1vacpump.Text & "," &
mech1spindle.Text & "," & mech1vacpumpserialnum.Text & "," &
mech1filterpartnum.Text & "," & mech2serialnum.Text & "," &
mech2vacpump.Text & "," & mech2spindle.Text & "," &
mech2vacpumpserialnum.Text & "," & mech2filterpartnum.Text & "," &
mech3serialnum.Text & "," & mech3vacpump.Text & "," & mech3spindle.Text
& "," & mech3vacpumpserialnum.Text & "," & mech3filterpartnum.Text &
"," & mech4serialnum.Text & "," & mech4vacpump.Text & "," &
mech4spindle.Text & "," & mech4vacpumpserialnum.Text & "," &
mech4filterpartnum.Text & "," & user1txt.Text & "," & user2txt.Text &
"," & user3txt.Text & "," & user4txt.Text & "," & user5txt.Text & "," &
user6txt.Text & "," & user7txt.Text & "," & user8txt.Text & "," &
user9txt.Text & "," & user10txt.Text & "," & user11txt.Text & "," &
user12txt.Text & "," & user13txt.Text & "," & user14txt.Text & "," &
user15txt.Text
5.
filename2 = "C:\Documents and Settings\Brian Bepristis\My
Documents\Visual Studio Projects\Multicamtemp.csv"
6.
Dim filename As String
7.
filename = "C:\Documents and Settings\Brian Bepristis\My
Documents\Visual Studio Projects\Multicamtemp2.csv"
8.
' Create an empty string array to return to caller.
9.
Dim lines() As String = {}
10.

11.
' Check to see if the file exists.
12.
If File.Exists(filename) Then
13.
' Open a stream reader to get the text from the file.
14.
Dim sr As New StreamReader(filename)
15.
' Read all the file text.
16.
Dim fileText As String = sr.ReadToEnd()
17.
' Close the stream reader
18.
sr.Close()
19.

20.
' Split the text into a string array delimited by
Carriage Return/Line Feed.
21.
lines = Split(fileText, vbCrLf)
22.

23.
' Return
24.
Dim fs As New FileStream(filename2, FileMode.Create)
25.
Dim sw As New StreamWriter(fs)
26.

27.
Dim foundBlank As Boolean
28.
For Each line As String In lines
29.
If line.Length > 0 Then
30.
line_num += 1
31.
If line_num = currecord Then
32.
sw.WriteLine(csv_text)
33.
' Reset blank line flag
34.
foundBlank = False
35.
Else
36.
sw.WriteLine(line)
37.
' Reset blank line flag
38.
foundBlank = False
39.
End If
40.
Else
41.
If Not foundBlank Then
42.
' Blank line: write first one
43.
sw.WriteLine(line)
44.
' Set flag to indicate that blank line
was found
45.
foundBlank = True
46.
End If
47.
End If
48.
Next
49.
sw.Close()
50.
fs.Close()
51.
End If
52.
If System.IO.File.Exists(filename2) = True Then
53.
If System.IO.File.Exists(filename) = True Then
54.
System.IO.File.Delete(filename)
55.
End If
56.
System.IO.File.Copy(filename2, filename)
57.
End If

May 11 '06 #2
not sure im new to vb so Im not sure what you mean

May 11 '06 #3
Nevermind figured it out there was some funky charicters in the csv
file stoping it..

Chars were --- NSB@ VL? ---

May 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by dunnm | last post: by
6 posts views Thread by KevinD | last post: by
40 posts views Thread by googler | last post: by
6 posts views Thread by arne.muller | last post: by
10 posts views Thread by Tyler | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.