VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 307,008 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,034 people online right now. Registration is fast and FREE... Join Now!




Annoying writeline when writing to a file.

2 Pages V  1 2 >  

Annoying writeline when writing to a file., Writeline creates an extra empty line for a reason.

trashr0x

7 Nov, 2009 - 08:57 AM
Post #1

New D.I.C Head
*

Joined: 27 Oct, 2009
Posts: 23


My Contributions
Hey guys, I am writing an array in a file with this loop.

CODE
sw.WriteLine("attributes")

For i = 0 To noatts - 1
            sw.WriteLine(attName(i))
        Next


This is creating weird results though.

Instead of:
attributes
att1
att2
att3

I am getting:
attributes
att1

att2

att3
(Notice the empty lines).

Anyone know why could that be? In other parts of the file writeline works well. It only creates extra lines on that specific section.

Btw: the file looks good if viewing by notepad, the extra lines appear when viewing in wordpad. Apparently that matters because when I reuse that file to read some values in my program, it crashes cause it reads those empty lines instead of the attributes.

This post has been edited by trashr0x: 7 Nov, 2009 - 09:01 AM

User is offlineProfile CardPM
+Quote Post


Martyr2

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 09:33 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,307



Thanked: 837 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Are you reading in a new line character and putting it in attName? If you are reading in the new line character from a file and putting it in the attName, you would be writing the new line character and then WriteLine would append another.

smile.gif
User is offlineProfile CardPM
+Quote Post

CapeCAD

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 09:38 AM
Post #3

D.I.C Head
**

Joined: 10 Aug, 2009
Posts: 60



Thanked: 14 times
My Contributions
QUOTE(trashr0x @ 7 Nov, 2009 - 10:57 AM) *

Hey guys, I am writing an array in a file with this loop.

CODE
sw.WriteLine("attributes")

For i = 0 To noatts - 1
            sw.WriteLine(attName(i))
        Next


This is creating weird results though.

Instead of:
attributes
att1
att2
att3

I am getting:
attributes
att1

att2

att3
(Notice the empty lines).

Anyone know why could that be? In other parts of the file writeline works well. It only creates extra lines on that specific section.

Btw: the file looks good if viewing by notepad, the extra lines appear when viewing in wordpad. Apparently that matters because when I reuse that file to read some values in my program, it crashes cause it reads those empty lines instead of the attributes.



How are you filling attName(i)? Does it have CRLF at the end of it?
User is offlineProfile CardPM
+Quote Post

trashr0x

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 09:38 AM
Post #4

New D.I.C Head
*

Joined: 27 Oct, 2009
Posts: 23


My Contributions
QUOTE(Martyr2 @ 7 Nov, 2009 - 09:33 AM) *

Are you reading in a new line character and putting it in attName? If you are reading in the new line character from a file and putting it in the attName, you would be writing the new line character and then WriteLine would append another.

smile.gif


I am writing the attname array in a file. One element per line.

Instead of getting

1
2
3

I'm getting

1

2

3

which makes no sense!

User is offlineProfile CardPM
+Quote Post

nmgod

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 09:40 AM
Post #5

D.I.C Head
**

Joined: 26 Mar, 2008
Posts: 93



Thanked: 11 times
My Contributions
try changing it to this


CODE
sw.WriteLine("attributes")

For i = 0 To noatts - 1
            sw.WriteLine(attName(i).Trim(CChar(ControlChars.NewLine)))
Next

User is offlineProfile CardPM
+Quote Post

trashr0x

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 09:47 AM
Post #6

New D.I.C Head
*

Joined: 27 Oct, 2009
Posts: 23


My Contributions
@ capeCAD:

The array is being filled from that exact file which I am filling the arrays (it's a "settings" file)

First I place them In a textbox:

CODE
For k = 0 To bound - 1
            myCaller.attName(k) = myCaller.sr.ReadLine
            txtAtt.Text = txtAtt.Text & myCaller.attName(k) & vbCrLf
                  Next

It's firstly placed in a textbox in case the user wants to edit something before actually loading it. Loading is here:


CODE
For k = 0 To bound - 1
            myCaller.attName(k) = txtAtt.Text.Split(vbCrLf)(k)
                     Next



@ nmgod:

Same problem.

attributes
alpha

beta

charlie

User is offlineProfile CardPM
+Quote Post

nmgod

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 09:53 AM
Post #7

D.I.C Head
**

Joined: 26 Mar, 2008
Posts: 93



Thanked: 11 times
My Contributions
upload the textfile
User is offlineProfile CardPM
+Quote Post

Martyr2

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 09:55 AM
Post #8

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,307



Thanked: 837 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Your problem is with the line txtAtt.Text.Split(vbCrLf)(k). If your token you are splitting on is at the end of the string, like the vbCrlf is at the end of the line, and you split it, it creates an empty piece at the end. You are adding that into your attNames array which is why you see the empty spaces. You are actually printing an empty token with a crlf on the end of it.

smile.gif
User is offlineProfile CardPM
+Quote Post

trashr0x

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 10:02 AM
Post #9

New D.I.C Head
*

Joined: 27 Oct, 2009
Posts: 23


My Contributions
My files are in a extension i created and viewed in notepad. Dreamincode wont let me upload but I can do something else.

This is the actual file before loading

[attributes]
9

[attribute names]
alpha
beta
charlie
delta
ernesto
forget
xerox
you
zero


After rewriting the file though

[attributes]
9

[attribute names]
alpha

beta

charlie

delta

ernesto

forget

xerox

you

zero


This post has been edited by trashr0x: 7 Nov, 2009 - 10:18 AM
User is offlineProfile CardPM
+Quote Post

nmgod

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 10:03 AM
Post #10

D.I.C Head
**

Joined: 26 Mar, 2008
Posts: 93



Thanked: 11 times
My Contributions
QUOTE(Martyr2 @ 8 Nov, 2009 - 02:55 AM) *

Your problem is with the line txtAtt.Text.Split(vbCrLf)(k). If your token you are splitting on is at the end of the string, like the vbCrlf is at the end of the line, and you split it, it creates an empty piece at the end. You are adding that into your attNames array which is why you see the empty spaces. You are actually printing an empty token with a crlf on the end of it.

smile.gif


Thats exactly what I was going to check when he uploaded the file, beat me to it lol.
User is offlineProfile CardPM
+Quote Post

trashr0x

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 10:04 AM
Post #11

New D.I.C Head
*

Joined: 27 Oct, 2009
Posts: 23


My Contributions
QUOTE(Martyr2 @ 7 Nov, 2009 - 09:55 AM) *

Your problem is with the line txtAtt.Text.Split(vbCrLf)(k). If your token you are splitting on is at the end of the string, like the vbCrlf is at the end of the line, and you split it, it creates an empty piece at the end. You are adding that into your attNames array which is why you see the empty spaces. You are actually printing an empty token with a crlf on the end of it.

smile.gif


So are you telling me that the error happening is..
attname(1) = blabla
attname(2) = empty
attname(3) = blabla
attname(4) = empty?

User is offlineProfile CardPM
+Quote Post

nmgod

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 10:06 AM
Post #12

D.I.C Head
**

Joined: 26 Mar, 2008
Posts: 93



Thanked: 11 times
My Contributions
QUOTE(trashr0x @ 8 Nov, 2009 - 03:04 AM) *

QUOTE(Martyr2 @ 7 Nov, 2009 - 09:55 AM) *

Your problem is with the line txtAtt.Text.Split(vbCrLf)(k). If your token you are splitting on is at the end of the string, like the vbCrlf is at the end of the line, and you split it, it creates an empty piece at the end. You are adding that into your attNames array which is why you see the empty spaces. You are actually printing an empty token with a crlf on the end of it.

smile.gif


So are you telling me that the error happening is..
attname(1) = blabla
attname(2) = empty
attname(3) = blabla
attname(4) = empty?


No you are getting
attname(1) = blabla{empty}
attname(2) = blabla{empty}

from what I understand

This post has been edited by nmgod: 7 Nov, 2009 - 10:07 AM
User is offlineProfile CardPM
+Quote Post

trashr0x

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 10:13 AM
Post #13

New D.I.C Head
*

Joined: 27 Oct, 2009
Posts: 23


My Contributions
Oh yeah you're true.. I tried debugging with this line
CODE
MsgBox("'" & attName(i) & "'")
while the file is created..

This is how the msgboxs look like:

'alpha'
'
beta'

..So how can I fix this?
User is offlineProfile CardPM
+Quote Post

nmgod

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 10:15 AM
Post #14

D.I.C Head
**

Joined: 26 Mar, 2008
Posts: 93



Thanked: 11 times
My Contributions
What is your code to load it to memory?
User is offlineProfile CardPM
+Quote Post

trashr0x

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 10:17 AM
Post #15

New D.I.C Head
*

Joined: 27 Oct, 2009
Posts: 23


My Contributions
I posted that before on post #6 of this thread
User is offlineProfile CardPM
+Quote Post

nmgod

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 10:27 AM
Post #16

D.I.C Head
**

Joined: 26 Mar, 2008
Posts: 93



Thanked: 11 times
My Contributions
upload the output, not as text but the actual file itself
User is offlineProfile CardPM
+Quote Post

trashr0x

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 10:30 AM
Post #17

New D.I.C Head
*

Joined: 27 Oct, 2009
Posts: 23


My Contributions
QUOTE(nmgod @ 7 Nov, 2009 - 10:27 AM) *

upload the output, not as text but the actual file itself


THE FOLLOWING ERROR(S) WERE FOUND
Upload failed. You are not permitted to upload a file with that file extension.

It's an extension I created for this program.. Normal CSV file though nothing special

User is offlineProfile CardPM
+Quote Post

nmgod

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 10:33 AM
Post #18

D.I.C Head
**

Joined: 26 Mar, 2008
Posts: 93



Thanked: 11 times
My Contributions
just change the extention to .txt
User is offlineProfile CardPM
+Quote Post

trashr0x

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 10:38 AM
Post #19

New D.I.C Head
*

Joined: 27 Oct, 2009
Posts: 23


My Contributions
Attached File  dreamcode.txt ( 575bytes ) Number of downloads: 11


This post has been edited by trashr0x: 7 Nov, 2009 - 10:39 AM
User is offlineProfile CardPM
+Quote Post

nmgod

RE: Annoying Writeline When Writing To A File.

7 Nov, 2009 - 10:43 AM
Post #20

D.I.C Head
**

Joined: 26 Mar, 2008
Posts: 93



Thanked: 11 times
My Contributions
ok try this
CODE
sw.WriteLine("attributes")

For i = 0 To noatts - 1
            sw.WriteLine(attName(i).Trim(CChar(ControlChars.Lf)))
Next

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 07:07AM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month