-
Nov 7th, 2021, 02:36 PM
#1
Thread Starter
Member
Windows Forms Stream Reader Problem
I am having trouble with my text files in VB.NET
Code:
Dim Source As New StreamReader(Sfile) 'Open the file for reading
Dim Destination As New StreamWriter(Dfile, True) 'Open the file for writting
Dim line As String
line = Source.ReadLine()
While (line IsNot Nothing)
Destination.WriteLine(line)
line = Source.ReadLine()
End While
Destination.Close()
Source.Close()
I am reading from one file and writing to another line by line.
The problem is with the last line of the file.
If the last line of the source file has text, the program will add a blank line after it in the destination file.
I can get around this by using:
Code:
Destination.Write(line)
line = Source.ReadLine()
If line IsNot Nothing Then 'Only add a carriage return if we are not at the end of the file
Destination.WriteLine()
End If
This will stop the blank line from being added to the destination file.
However, if the source file ends with a blank line, this will also stop the blank line from being transferred to the destination file.
Also, if I use:
Code:
Dim lineCount = File.ReadAllLines(Sfile).Length
The lineCount = 3515 when the source file ends with a line of text (which is correct)
and 3515 (the same) when a blank line (or carriage return) is added after the last line of text.
I just want to be able to write each line verbatim.
If the source file ends with text I want the destination file to end with text, and if the source file ends with a blank line I want the destination file to end with a blank line.
Is that too much to ask?
-
Nov 7th, 2021, 03:03 PM
#2
Re: Windows Forms Stream Reader Problem
I think you're over thinking it:
Code:
Dim readText() As String = File.ReadAllLines(sourcePath)
File.WriteAllLines(destinationPath, readText)
But if, ll you're doing is copying the contents of the file,why not just copy the file? Why go through the process of reading & writing? Seems convoluted to me.
Code:
File.Copy(sourcePath, destinationPath)
-tg
-
Nov 7th, 2021, 03:06 PM
#3
Thread Starter
Member
Re: Windows Forms Stream Reader Problem
I want to do some string manipulation on the source lines before they are written to the destination, but I haven't gotten that far yet because I can't get the "simple" stuff to work.
-
Nov 7th, 2021, 03:24 PM
#4
Re: Windows Forms Stream Reader Problem
ReadAllLined and WriteAllLines... it doesn't get any simpler than that.
-tg
-
Nov 7th, 2021, 04:21 PM
#5
Addicted Member
Re: Windows Forms Stream Reader Problem
Destination.WriteLine(mystring) appends a line terminator to whatever is presented, try using Destination.Write(mystring & vbcrlf) or Destination.Write(mystring) as necessary.
-
Nov 7th, 2021, 04:56 PM
#6
Thread Starter
Member
Re: Windows Forms Stream Reader Problem
Originally Posted by techgnome
ReadAllLined and WriteAllLines... it doesn't get any simpler than that.
-tg
So I have to read the entire file into a string array, do the changes, and then write it back out to a file?
It seemed simpler to read and write each line directly from and to the files.
-
Nov 8th, 2021, 11:14 AM
#7
Addicted Member
Re: Windows Forms Stream Reader Problem
Code:
Dim Source As New StreamReader(Sfile)
Dim Destination As New StreamWriter(Dfile, True)
Do While Source.Peek() >= 0
Destination.WriteLine(Source.ReadLine())
Loop
Destination.Close()
Source.Close()
-
Nov 8th, 2021, 11:24 AM
#8
Re: Windows Forms Stream Reader Problem
Originally Posted by Nibbled
It seemed simpler to read and write each line directly from and to the files.
If you're going to ready/write every line then it makes more sense to load the file in memory in one chunk, loop over the in-memory collection, and then write everything back in one chunk. E.g.:
Code:
Dim lines() As String = IO.File.ReadAllLines("source.txt")
Dim massagedLines(lines.Length - 1) As String
For index As Integer = 0 To lines.Length - 1
Dim line As String = lines(index)
' do something...
massagedLines(index) = line ' or massaged data...
Next
IO.File.WriteAllLines("destination.txt", massagedLines)
With this approach you're less likely to have any IO exceptions and I believe it would also be quicker.
-
Nov 8th, 2021, 01:25 PM
#9
Thread Starter
Member
Re: Windows Forms Stream Reader Problem
bmwpete and dday9,
Thanks for your suggestions.
Unfortunately, both solutions still add a blank line to the destination file when there isn't one in the source file.
So, I have had another idea.
Once I have my destination file, can I read the last line of the source file to see if it is blank, and if not, delete the last line of the destination file?
-
Nov 8th, 2021, 01:50 PM
#10
Re: Windows Forms Stream Reader Problem
Originally Posted by Nibbled
Unfortunately, both solutions still add a blank line to the destination file when there isn't one in the source file.
Nope. You're doing something to add the blank line. Look at this example: https://dotnetfiddle.net/Qq61z5
Without seeing your code I cannot tell you where you are adding the extra line.
-
Nov 8th, 2021, 02:10 PM
#11
Thread Starter
Member
Re: Windows Forms Stream Reader Problem
Nope. You're doing something to add the blank line.
I'm not adding any lines.
I have found what the problem is, but I am still no nearer to a solution.
If you look at my original post, I said: "The lineCount = 3515 when the source file ends with a line of text (which is correct)
and 3515 (the same) when a blank line (or carriage return) is added after the last line of text."
All the solutions which have been posted all add a carriage return or line feed to the end of each line. The line count stays the same because another line has not been added, just the return at the end of the line.
I need to find a way of NOT adding the return character if the source files last line does not have one.
Again, in my original post, I showed a fiddle for this, but unfortunately, that workaround also removes the carriage return character when it is supposed to be there.
Last edited by Nibbled; Nov 8th, 2021 at 02:13 PM.
-
Nov 8th, 2021, 02:21 PM
#12
Re: Windows Forms Stream Reader Problem
Perhaps I'm misunderstanding you. If a file's last line is not an empty string, then you want to add a carriage return, but not a line feed?
-
Nov 8th, 2021, 11:16 PM
#13
Re: Windows Forms Stream Reader Problem
I'm curious as to why it's such a problem.
-tg
-
Nov 8th, 2021, 11:19 PM
#14
Re: Windows Forms Stream Reader Problem
But that aside.... the issue is where the EOF is falling...
Code:
Line 1
Line 2
Line 3
Line 4
{eof}
vs
Code:
Line 1
Line 2
Line 3
Line 4{eof}
-tg
-
Nov 9th, 2021, 02:55 AM
#15
Thread Starter
Member
Re: Windows Forms Stream Reader Problem
Originally Posted by techgnome
But that aside.... the issue is where the EOF is falling...
-tg
EXACTLY!
Although the line count is the same in both instances when you view the files in a text editor the destination file does not match the source file if the EOF is in the wrong place.
I am surprised that this is such a difficult thing to achieve.
-
Nov 9th, 2021, 08:15 AM
#16
Re: Windows Forms Stream Reader Problem
It's not really that difficult to achieve... I'm just wondering if you're tilting windmills here...
I suppose - and I'k just spitballing here - you could use ReadAllLines to read the data in, do your manipulations, then loop through the array, using a WriteLine to write the data, keeping track of which index you're on (or checking for the last index) and when you get to the last index element, use a Write instead of a WriteLine. Again, theory, untested, I don't have a way to check to see if this might work, but it might give you or someone else something to work off of.
-tg
-
Nov 9th, 2021, 11:46 AM
#17
Thread Starter
Member
Re: Windows Forms Stream Reader Problem
Originally Posted by techgnome
It's not really that difficult to achieve... I'm just wondering if you're tilting windmills here...
I suppose - and I'k just spitballing here - you could use ReadAllLines to read the data in, do your manipulations, then loop through the array, using a WriteLine to write the data, keeping track of which index you're on (or checking for the last index) and when you get to the last index element, use a Write instead of a WriteLine. Again, theory, untested, I don't have a way to check to see if this might work, but it might give you or someone else something to work off of.
-tg
As the "index" or line count, is the same regardless of where the EOF is this approach doesn't work.
Also, if "It's not really that difficult to achieve", then why has nobody been able to come up with a solution?
-
Nov 9th, 2021, 01:08 PM
#18
Re: Windows Forms Stream Reader Problem
Did you even try it? Using a WRITE rather than a WriteLINE for the last line? That's what the difference is. That's all.
WriteAllLine will use a WriteLine by default... so that's out... so will a simple loop with a WriteLine... that's why I said to read it all in, do your processing, get the final length of the array, and when you go to write it out, write out each element using WriteLine until you get to the last one, then use a Write.. then flush and close hte file.... that should put the EOF right there on the same line as the last line and not add any additional characters.
The other thing I have to question is how you're checking that the cr/lf is being added in the first place... some text editors will add that extra line at the end automatically if there isn't one. So make sure the blame is in the right place (take it from someone that spent two days trying to debug why a REST request was being rejected from Postman, only to find out that the url should have been https and not http.... two days I spent on that...)
-tg
-
Nov 9th, 2021, 05:04 PM
#19
Re: Windows Forms Stream Reader Problem
Here.... does this help?
Code:
Dim path as String = "Path to the file to read"
Dim path2 as String = "Path to the file to write to"
Dim readText() As String = File.ReadAllLines(path)
Dim writer as new StreamWriter(path2)
For idx as Integer = 0 to readText.length-1
Dim theString as String = readText(idx)
'Do your manipulations here
if idx = readText.Length - 1 Then
writer.Write(theString)
else
writer.WriteLine(theString)
end if
Next
Like I said... it's not that difficult... read in all the text... loop through it... do your manipulations... check to see if you're on the last one, if you aren't then use WriteLine, otherwise use Write.
fiddle - https://dotnetfiddle.net/A8VM7v
-tg
Last edited by techgnome; Nov 9th, 2021 at 05:10 PM.
-
Nov 10th, 2021, 02:59 AM
#20
Thread Starter
Member
Re: Windows Forms Stream Reader Problem
You guys are sending me around in circles.
Did you read my original post?
I can get around this by using:
Code:
Destination.Write(line)
line = Source.ReadLine()
If line IsNot Nothing Then 'Only add a carriage return if we are not at the end of the file
Destination.WriteLine()
End If
This will stop the blank line from being added to the destination file.
However, if the source file ends with a blank line, this will also stop the blank line from being transferred to the destination file.
Okay so you are doing it slightly differently, but the result is the same.
Not that difficult eh?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|