Streamwriter adding unwanted characters to the beginning of the line - vb.net

I've written a program to write out an automated, delimited invoice file, and it seems to work properly, the file looks correct in notepad. However, when the file is received on the other end, there are a couple of extra characters at the beginning.
The code is basically
fWriter = My.Computer.FileSystem.OpenTextFileWriter(OutputPath)
LineString = 'insert line here
fWriter.WriteLine(LineString)`
the screenshot from the client has a three odd shaped characters at the beginning. They aren't in the input string, and I'm lead to believe it's because the OpenTextFileWriter isn't writing ascii, but it's a flat text file, or it's supposed to be.
Any help would be appreciated.

Related

Read and split line by line in text file

I am trying to read a text file from my applications resources. For each line in this text file I want to split the text before and after the comma.
Each line in txt file looks like this:
-125.325235,4845636
My issue is that the function loops and does not end constantly repeating the for each statement
For Each Line As String In My.Resources.CompanyBases
MsgBox(My.Resources.CompanyBases.Split(","c).First)
MsgBox(My.Resources.CompanyBases.Split(","c).Last)
Next
Firstly, don't ever get a resource over and over like that. Those properties are not "live". Every time you get the property, the resource has to be extracted from your assembly. If you need to use the value multiple times, get the property once and assign it to a variable, then use that variable over and over.
Secondly, you're not getting a file. The whole point of resources is that they are not distinct files but rather data compiled into your assembly. It's just a String like any other. How would you usually split a String on line breaks?
Finally, you have a For Each loop with a loop control variable Line, yet you never use that variable inside the loop. It should be Line that you're splitting inside the loop, not the resource property containing all the lines.
For Each line In My.Resources.CompanyBases.Split({Environment.NewLine}, StringSplitOptions.None)
Dim fields = line.Split(","c)
Debug.WriteLine(fields(0))
Debug.WriteLine(fields(1))
Next
Note that, if you're using .NET Core, Split will accept a String as well as a String array.

How to print certain lines of an input text file to another file in VB

I have a large file which i am writing to a smaller file in VB, i just dont know how to select lines from an input file that i opened.
I would like to keep the first 12 lines of the large file and them copy every 3rd line into the new output.
Any help?!
You can do this with a StreamReader - just open one on the file, then you can call ReadLine() on it as many times as you need.
Easiest implementation would probably be a 0-to-11 (or 1-to-12) For loop, then a While where you read 2 and ignore them, then read the 3rd and write it.
Writing the new file can be done with StreamWriter, which just has a WriteLine() method to write the text.
The StreamReader and StreamWriter are generally the easiest ways to read and write text files.

Replace a 'bel' character in a file in vb.NET

I've come across a character in one of my data feeds, which I have never encountered before
The images above are the data feed in Notepad++ and Notepad view. As you can see it appears as 'BEL' in Notepad++ and a sort of 'bullet point' in Notepad.
How would I go about replacing this character in vb.NET?
I've tried a simple replace in a SSIS Script Task by copying and pasting the character into the replace function, e.g.
text = text.Replace("copy and pasted character", "")
and this gives this error
All help is extremely appreciated,
Thanks
I’ve got no idea what SSIS is but since you wanted to know a solution in VB.NET, the code you’ve tried will work here. That is:
text = text.Replace("copy and pasted character", "")
will work just fine in VB. Alternatively, you can use the following:
text = text.Replace(Chr(7).ToString(), "")
Find out what the Ascii value of the character is and then use the Chr function to eliminate it
i.e.
text = text.Replace(Chr(n), "")
[Bell] is probably character 7

New words after pdf copy-paste

I have a pdf file. Then i select and copy "K([2.2.2]crypt)]5[Co2Sn17".
But in clipboard there is "KACHTUNGTRENUNG([2.2.2]crypt)]5ACHTUNGTRENUNG[Co2Sn17".
Any ideas what is "ACHTUNGTRENUNG"? Is it a kind of protection?
There likely are a few extra (invisible) characters in the file. When you copy the text, the application you use to copy translates the characters in the PDF file into something that can be stored on the clipboard. Most likely that happens by translating every character into the unicode string stored in the PDF file for that character in the used font.
For most normal characters the Unicode string should be the same as the character you visually see; here you probably have invisible spaces in the PDF file that are called "achtungtrenung" in the font.
If you have the PDF file available somewhere, I'll be happy to take a look and verify this is indeed what is happening.
It's extra characters between lines.
You can try the PDF Copy Paste software, and see if your desired portion can be converted to text of your preferences.

Remove "Invisible" Control Characters in VB.Net

I am currently reading in a text file in VB.Net using
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText(file)
File contains several lines of text and when I read in the text file, it knows that they are on separate lines and prints them out accordingly.
However, when I try to split fileReader into an array of the different lines, the line break seems to stay there, even if I use Split(ControlChars.Cr) or Split(ControlChars.NewLine). It will successfully split it into the separate lines but when I display it, it will "push" the text down a line, like the line break is still there...
Does anyone have any ideas on what is going on and how I can remove these "invisible" control chars.
Text File:
Test1
Test2
Test3
Test4
fileReader:
Test1
Test2
Test3
Test4
lines() printout
Test1
Test2
Test3
Test4
Use trim() on each line, it'll remove extraneous whitespace.
The System.IO.File class has a ReadAllLines method that will actually give you back an array of strings, one per line.
If that method doesn't work, either, I would examine exactly what bytes are causing you issues. In the watch window, you can do a System.Text.Encoding.ASCII.GetBytes (sampleLine) and examine exactly what you are working with.
I'm assuming you are using ASCII encoding, if not, you'll need to swap out ASCII with the correct option, and then modify your file read to read based on that encoding, as well.
As mentioned use the Readalllines method to have it split automatically.
The problem you are having is PC ASCII files are usually split with a carriage return and a new line, splitting on just one will leave the other. You can split and trim as mentioned or use the other split that splits on strings instead of chars.
dim s() as string = Split(fileReader ,vbCrLf)
Trim will remove spaces from the data as well, depending on your situation that could be a problem for you.
Ran into a similar problem recently. The Trim() doesnt work because the extra lines are already there after doing the split (or using File.ReadAllLines). Here's what worked for me:
Dim allText As String = System.IO.File.ReadAllText(filePath)
allText = allText.Replace(Chr(13), "")
Dim lines As String() = allText.Split(vbLf)
Chr(13) is the Control-M character that result in extra lines using Split() or File.ReadAllLines.