VB.NET and creating text file

G

Guest

Hi,

I want to create a text file in the following format:
Name=yyy
Amount=zzz

and so on. The file is key-value pair and each pair appears on next line.

Now part of the data comes from the grid whereas rest I pick up from the
database. And I wrote the following code:

Dim strmExport As System.IO.StreamWriter
strmExport = New System.IO.StreamWriter &
_(objInterfaceINIData.strWorkingDirectory & mstrTempFileInput)

strmExport.AutoFlush = True

strLine = ""
strmExport.WriteLine(strLine)

strLine = "Name=" & """" & strName.Trim & """"
'Note Double quotes to take of any single quote in the name

strmExport.WriteLine(strLine)

strLine = "Amount=" & dblAmount
strmExport.WriteLine(strLine)

And so on.
Now the client says that my output file has tab chars (ascii 9). But in my
code, I am not using tab anywhere.
Now:
a. Does WriteLine add a tab char? But I cannot use Write method since I need
the key-value pair on separate lines. How do I remove tab keys?
b. I opened the file in notepad but did not see tab key. Also tried in Word.
Is there any other way to find if tab key exist in the output file?

Regards,
VJ
 
J

James Jardine

VJ said:
Hi,

I want to create a text file in the following format:
Name=yyy
Amount=zzz

and so on. The file is key-value pair and each pair appears on next line.

Now part of the data comes from the grid whereas rest I pick up from the
database. And I wrote the following code:

Dim strmExport As System.IO.StreamWriter
strmExport = New System.IO.StreamWriter &
_(objInterfaceINIData.strWorkingDirectory & mstrTempFileInput)

strmExport.AutoFlush = True

strLine = ""
strmExport.WriteLine(strLine)

strLine = "Name=" & """" & strName.Trim & """"
'Note Double quotes to take of any single quote in the name

strmExport.WriteLine(strLine)

strLine = "Amount=" & dblAmount
strmExport.WriteLine(strLine)

And so on.
Now the client says that my output file has tab chars (ascii 9). But in my
code, I am not using tab anywhere.
Now:
a. Does WriteLine add a tab char? But I cannot use Write method since I
need
the key-value pair on separate lines. How do I remove tab keys?
b. I opened the file in notepad but did not see tab key. Also tried in
Word.
Is there any other way to find if tab key exist in the output file?

Regards,
VJ

I don't know about any of the write methods adding a tab key. That seems
sort of strange.. What about if you used the write method instead of the
writeline method and after eacy pair append on Environment.NewLine
That should do the same as WriteLine would

So ex. your line strLine = "Name=" & """" & strName.Trim & """"
might look like

strLine = "Name=" & """" & strName.Trim & """" & Environment.NewLine
 
D

Dom

You may be getting tab characters from the input you are reading...

Writeline doesn't write out tab characters that you wouldn't expect.

It's trivial, but you might also want to put this into a function to
make it easier to test:

function CreateKeyValuePair(key as string, value as string) as String
dim result as String
result += key.trim
result += "="
result += value.trim
end function

Your NUnit test code would look like:
public sub TestCreateKeyValuePair()
assert_equal("Name=xxx", CreateKeyValuePair("Name", "xxx"))
assert_equal("Amount=yyy", CreateKeyValuePair("Amount", "yyy"))
end sub

Your client code would look like:
strmExport.WriteLine(CreateKeyValuePair("Name", "xxx"))
strmExport.WriteLine(CreateKeyValuePair("Amount", "yyy"))

HTH

Dominique
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top