Link to home
Start Free TrialLog in
Avatar of cltmmm
cltmmmFlag for United States of America

asked on

Streamwriter isn't writing what it's being fed

Hi. I am reading a flat file into a string variable. I then want write that data to another flat file. My code simply looks like this:

 
        Dim strFile As String = String.Empty
        Dim sw As IO.StreamWriter
        Dim sr As New IO.StreamReader("C:\test\InFile.trn")
        strFile = sr.ReadToEnd
        sw = New IO.StreamWriter("C:\test\OutFile.trn")
        sw.Write(strFile)
        sw.Close()

Open in new window


Within the data in InFile.trn (I pasted the contents of InFile.trn at the bottom of this question) there is a vertical pipe (hex(A6)). But in Outfile.trn the vertical pipe (A6) has been converted to � (hex characters EF, BF and BD ). How do I stop this translating from happening so that the output file looks like the input file? (PS - There is more logic that happens in the real program, which is why I am just not doing a file copy).

Thanks!

Here is the data in InFile.trn:
130212~0217~U~00401~000002499~0~P~¦\GS
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

hmmm .. why don't you just copy the file? why "read in" and "write" ?

anyhow... the issue is that you are doing a "string" reading, and not a "binary" reading, so the read/write might "translate" some characters indeed.

so, short answer is:
* if you don't need to inspect the data, and just want to copy the file "as is": use the File.Copy method instead of StreamReading/Writing

* if you need to inspect (and eventually modify) the data, you need to read it in "binary"
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx
http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx
http://msdn.microsoft.com/en-us/library/36b93480.aspx
Avatar of cltmmm

ASKER

Thanks for the reply. Yes there is more going on in the app than just reading and writing a file. The input file can contain data which could be any particular format. The goal of my program is to read the input file and if it is a single-line file I need to determine what the source's ending delimiter is and change that to a line feed. For example, If this is the data in my input file:
000~111~222~333~MMM\GS1234\Hello\Good-Bye
Suppose our rule is that if the length of the data line is > 19 then I know the file has no line feeds and I need to insert them by replacing whatever character I find in position 19. In this example the "delimiter" character is "\". So I need to translate all "\" into chr(10) and write that out. My output file would look like this:
000~111~222~333~MMM
GS1234
Hello
Good-Bye

This has worked fine until I got a file that contained a vertical pipe in the data. Example:
000~111~222~333~MM¦\GS1234\Hello\Good-Bye
When I process that data I get the following output:
000~111~222~333~MM�
GS1234
Hello
Good-Bye

That is the problem I am trying to solve. Will reading the input file in binary do the trick for me? If so, which binaryreader read method would you suggest I use?
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cltmmm

ASKER

Thanks for your help.

I changed my streamreader to this:
Using reader As New IO.StreamReader(path, System.Text.Encoding.UTF7)
       contents = reader.ReadToEnd
End Using

Open in new window

That gets me a bit closer. Now when it reads a vertical pipe (¦) it writes out ¦. I am going to continue to massage this thing and will report back on my progress. However, if someone can tell me what I'm doing wrong I would greatly appreciate it.

Thanks again.
what about UTF8 or UTF16?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cltmmm

ASKER

The combination of angelIII's suggestion and a bit of testing on my part got us the solution.