|
-
Feb 16th, 2012, 01:04 PM
#1
Thread Starter
New Member
[RESOLVED] Writing back to a delimited text file
Hello, I am new to the VB Forums. I searched the forums for an answer, but could not find one that exactly fit my issue. I apologize if I missed it.
What I am doing is I'm pulling strings of text from a backslash-delimited text file and throwing it into a (Of String, Array) dictionary. This info is then displayed, added to, removed from, through the use of the form. Changes to the entries (new, old, or otherwise) then need to be written back in the same delimited format to the text file.
Everything seemingly works aside from one thing: every time I write back to the text file, its contents are erased and nothing is written in its place.
Here is my StreamWriter code:
Code:
Public Function WriteDataToFile(ByVal FilePath As String) As Boolean
'write all data back to the inventory file
Dim w = New StreamWriter(FilePath)
Dim entry As New KeyValuePair(Of String, Array)
For Each entry In Inventory.dict
w.Write(CStr(entry.Key & "\" & entry.Value(0, 0) & "\" & entry.Value(1, 0) & _
"\" & entry.Value(2, 0) & "\" & entry.Value(3, 0) & "\" & entry.Value(4, 0)))
w.WriteLine()
Next
Return True
w.Close()
End Function
Here is my form:

My 'close' button handler (changes are saved when this is clicked to close the form):
Code:
Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
'write data back to file
InvFile.WriteDataToFile(FilePath)
'close application
Close()
End Sub
I can provide further code if it would help, such as for adding new entries, displaying entries, etc. I just didn't want to add it right from the start and make this post unnecessarily more visually cluttered.
All of my fields are of type String. It seemed to make the strictly typed dictionary happier. The first delimited entry in each entry of the text file is the key, the following 5 go into the array. That's all I can think of for now.
Thank you in advance for trying to help!
-
Feb 16th, 2012, 01:37 PM
#2
Re: Writing back to a delimited text file
You have to flush the StreamWriter before closing.
-
Feb 16th, 2012, 01:43 PM
#3
Thread Starter
New Member
Re: Writing back to a delimited text file
 Originally Posted by Niya
You have to flush the StreamWriter before closing.
Could you elaborate a bit? I looked at VS after reading your post, and I see the Flush and AutoFlush commands, but I'm not familiar with them so I'm not sure how to implement them. Is it something along this line?
Code:
For Each entry In Inventory.dict
w.Write(CStr(entry.Key & "\" & entry.Value(0, 0) & "\" & entry.Value(1, 0) & _
"\" & entry.Value(2, 0) & "\" & entry.Value(3, 0) & "\" & entry.Value(4, 0)))
w.WriteLine()
w.Flush()
Next
Thanks.
-
Feb 16th, 2012, 01:52 PM
#4
Re: Writing back to a delimited text file
Almost....more like this:-
vbnet Code:
Public Function WriteDataToFile(ByVal FilePath As String) As Boolean 'write all data back to the inventory file Dim w = New StreamWriter(FilePath) Dim entry As New KeyValuePair(Of String, Array) For Each entry In Inventory.dict w.Write(CStr(entry.Key & "\" & entry.Value(0, 0) & "\" & entry.Value(1, 0) & _ "\" & entry.Value(2, 0) & "\" & entry.Value(3, 0) & "\" & entry.Value(4, 0))) w.WriteLine() Next Return True w.Flush()' <--------Flush here w.Close() End Function
You flush just before closing. Flushing every time you write something is inefficient.
-
Feb 16th, 2012, 01:53 PM
#5
Frenzied Member
Re: Writing back to a delimited text file
there is a function called join() its the opposite of split
in the old worlds you would just say string=join(array,"\") to achieve your string shape
it must still exist but i dont know the current syntax and i am a bit too busy to search for it just know!
hope tht helps your coding a little
-
Feb 16th, 2012, 01:59 PM
#6
Thread Starter
New Member
Re: Writing back to a delimited text file
Okay, never mind on that last post. I tried the Flush method as I put it in the Code box, and it worked. However, now I have a new issue. Everything works fine unless I try closing the form with more Dictionary entries(key, value) in place than were there when I first opened the form. Such as, if I open the form, create a new item/entry, then save it back to the dictionary as a new entry, I get a RankException. It says "Attempted to operate on an array with the incorrect number of dimensions."
Here's my code to add to the dictionary:
Code:
Private Sub btnAddItem_Click(sender As System.Object, e As System.EventArgs) Handles btnAddItem.Click
'add current item to the dictionaries
Try
Dim v As Array = {txtDescription.Text, txtDaily.Text, _
txtWeekly.Text, txtMonthly.Text, txtQuantity.Text}
Inventory.dict.Add(cboIdNumber.Text, v)
Catch ex As Exception
MessageBox.Show("Item has already been added, or already exists in the database.", "Error")
Exit Sub
End Try
End Sub
I don't get this error if I remove just as many entries as I'm adding, so it's merely expanding on the overall size of the array that's the issue.
My streamwriter code is the same as I show in my code box above regarding the addition of Flush. Any ideas?
-
Feb 16th, 2012, 02:01 PM
#7
Thread Starter
New Member
Re: Writing back to a delimited text file
 Originally Posted by incidentals
there is a function called join() its the opposite of split
in the old worlds you would just say string=join(array,"\") to achieve your string shape
it must still exist but i dont know the current syntax and i am a bit too busy to search for it just know!
hope tht helps your coding a little
The flush command seemed to work, however I will keep your suggestion in mind for future use. I was actually a bit curious if there was a way that I could simplify the code instead of writing in every single delimiter. It seems you answered that question without me even having to ask it. Thanks
-
Feb 16th, 2012, 02:04 PM
#8
Re: Writing back to a delimited text file
Oh shoot, I just realized that you made the mistake in your WriteDataToFile procedure of returning True before closing and flushing. You should fix that else the stream wont actually close and flush because the function would return before that.
Fixed:-
vbnet Code:
Public Function WriteDataToFile(ByVal FilePath As String) As Boolean 'write all data back to the inventory file Dim w = New StreamWriter(FilePath) Dim entry As New KeyValuePair(Of String, Array) For Each entry In Inventory.dict w.Write(CStr(entry.Key & "\" & entry.Value(0, 0) & "\" & entry.Value(1, 0) & _ "\" & entry.Value(2, 0) & "\" & entry.Value(3, 0) & "\" & entry.Value(4, 0))) w.WriteLine() Next w.Flush()' <--------Flush here w.Close() Return True End Function
Last edited by Niya; Feb 16th, 2012 at 02:08 PM.
-
Feb 16th, 2012, 02:06 PM
#9
Re: Writing back to a delimited text file
As for your other problem. You declared it as type Array instead of the type you want. It should be:-
vbnet Code:
Private Sub btnAddItem_Click(sender As System.Object, e As System.EventArgs) Handles btnAddItem.Click 'add current item to the dictionaries Try Dim v() As String = {txtDescription.Text, txtDaily.Text, _ txtWeekly.Text, txtMonthly.Text, txtQuantity.Text} Inventory.dict.Add(cboIdNumber.Text, v) Catch ex As Exception MessageBox.Show("Item has already been added, or already exists in the database.", "Error") Exit Sub End Try End Sub
-
Feb 16th, 2012, 02:08 PM
#10
Thread Starter
New Member
Re: Writing back to a delimited text file
 Originally Posted by Niya
Oh shoot, I just realized that you made the mistake in your WriteDataToFile procedure of returning True before closing and flushing. You should fix that else the stream wont actually close and flush because the function would return before that.
Oh, you're right. I guess I didn't catch that before. It wasn't giving me issue before, but nonetheless I'm sure it's good to change it. I ended up just switching around the close() and Return True statements.
-
Feb 16th, 2012, 02:13 PM
#11
Re: Writing back to a delimited text file
Its probably what initially caused the problem. When I answered in my first post it was immediately apparent to me that a flush was missing and I just jumped on that without looking at the code thoroughly enough. I've had issues with not flushing before but I believe that calling Close flushes before closing.
UPDATE:
Ok, I've just confirmed in Reflector that calling Close does indeed flush the writer. So what caused your trouble in the first place was that misplaced Return statement
Last edited by Niya; Feb 16th, 2012 at 02:19 PM.
-
Feb 16th, 2012, 02:29 PM
#12
Re: Writing back to a delimited text file
Oh bdbc I also addressed that other problem you had in case you missed the post.
-
Feb 16th, 2012, 02:35 PM
#13
Thread Starter
New Member
Re: Writing back to a delimited text file
After all of that, I was still getting a RankException. The change you recommended to me in the AddButton though clued me into something. For some reason I was getting a two-dimensional array from my original dictionary entries, but my AddButton was adding a one-dimensional array to it. I had to figure out why my dictionary was becoming two-dimensional in the first place.
It ended up being that I was declaring my dictionary as (Of String, Array), instead of (Of String, String()). The Array declaration for the dictionary value field was doubling up the dimensions. Essentially, I was making an array, then throwing it into the Array dictionary value field, making it an array within an array, or in other words a two-dimensional array.
I went back and changed the dictionary to (Of String, String()), and all 3 of my 'KeyValuePair(Of String, Array)' to (Of String, String()). That did it.
So after the changes you recommended, and the changes just mentioned, it now works as intended. Thank you!
P.S. Is there a Thank You button or something similar in these forums?
Edit: I see the 'Rate this post' and 'Add to reputation' links.
Last edited by bdbc78; Feb 16th, 2012 at 02:48 PM.
-
Feb 16th, 2012, 02:38 PM
#14
Thread Starter
New Member
Re: Writing back to a delimited text file
 Originally Posted by Niya
Its probably what initially caused the problem. When I answered in my first post it was immediately apparent to me that a flush was missing and I just jumped on that without looking at the code thoroughly enough. I've had issues with not flushing before but I believe that calling Close flushes before closing.
UPDATE:
Ok, I've just confirmed in Reflector that calling Close does indeed flush the writer. So what caused your trouble in the first place was that misplaced Return statement
I just tried removing the Flush() statement and running it again, after having made all of the other changes. It does in fact work normally now, without having to call Flush().
-
Feb 16th, 2012, 02:48 PM
#15
Re: Writing back to a delimited text file
 Originally Posted by bdbc78
After all of that, I was still getting a RankException. The change you recommended to me in the AddButton though clued me into something. For some reason I was getting a two-dimensional array from my original dictionary entries, but my AddButton was adding a one-dimensional array to it. I had to figure out why my dictionary was becoming two-dimensional in the first place.
It ended up being that I was declaring my dictionary as (Of String, Array), instead of (Of String, String()). The Array declaration for the dictionary value field was doubling up the dimensions. Essentially, I was making an array, then throwing it into the Array dictionary value field, making it an array within an array, or in other words a two-dimensional array.
I went back and changed the dictionary to (Of String, String()), and all 3 of my 'KeyValuePair(Of String, Array)' to (Of String, String()). That did it.
So after the changes you recommended, and the changes just mentioned, it now works as intended. Thank you!
P.S. Is there a Thank You button or something similar in these forums?
Glad to help .....the rate this post link on the left is the thank you button of this forum 
GL with everything.
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
|