[RESOLVED] Write To File With Append Creates New Line when it shouldn't
I have tried searching the forums for an answer but i could not find what i was looking for,
I'm basically trying to Write to a text file using the Append instead of Output function so that it puts all of the text on the same line.
However after 81 characters in the text document a new line is created and the remaining string that should have been on the first line starts on a new line.
Things i have tried:
Taking off word wrap on notepad
Putting a ";" at the end of each line
everything works fine, except depending on the length of account(i).ip and account(i).date the string will start a new line regardless of the code.
Is this a fault of notepad? Or is it something to do with my code? If there is no solution i may have to try using a SQL server or something which is too overboard for my small project. Thanks heaps for anyone that has any ideas.
Here is my code:
Code:
Public Sub write_data()
Open App.Path & "\accounts.dat" For Append As #1
For i = 0 To totalaccounts - 1
Print #1, account(i).firstname & "¿";
Print #1, account(i).surname & "¿";
Print #1, account(i).username & "¿";
Print #1, account(i).password & "¿";
Print #1, account(i).email & "¿";
Print #1, account(i).compUser & "¿";
Print #1, account(i).compName & "¿";
Print #1, account(i).ip & "¿";
Print #1, account(i).date; vbCrLf
Next i
Close 1
End Sub
This is also the text that gets written to my output file (accounts.dat) as it appears in notepad:
Re: Write to File, When appending, goes to new line Problem
Originally Posted by opus
?? Using WordWrap to OFF in NotePad will show all entries in a single line to me ???
Thanks for your reply, i did already try that though, and even with word wrap off, it seems to be when the string gets written to file that it seperates the string onto the second line
Re: Write to File, When appending, goes to new line Problem
i attached accounts.dat , it is actually a .txt document as i could not upload .dat file.
the way the information appears in the file is exactly how the program writes it to file but i have changed the name / email / ip for privacy reasons. thanks
Re: [UNRESOLVED] Write To File With Append Creates New Line when it shouldn't
Do you get different results doing something like this?
Code:
Public Sub write_data()
Dim strData(8) As String
Open App.Path & "\accounts.dat" For Append As #1
For i = 0 To totalaccounts - 1
strData(0) = account(i).firstname
strData(1) = account(i).surname
strData(2) = account(i).username
strData(3) = account(i).password
strData(4) = account(i).email
strData(5) = account(i).compUser
strData(6) = account(i).compName
strData(7) = account(i).ip
strData(8) = account(i).Date
Print #1, Join(strData, "¿") & vbCrLf
Next i
Close 1
End Sub
Re: [UNRESOLVED] Write To File With Append Creates New Line when it shouldn't
I see your problem, however??????????
The linebreak comes directly after the IP-address and BEFORE the "¿" which is set in the same code line as the IP. for this reason I think it is caused by the IP-address. How is it defined?, does this variable hold more then what we expect.
Do a test with several IP's for one (expected) line, do they also show this linebreak behaviour?
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Re: [UNRESOLVED] Write To File With Append Creates New Line when it shouldn't
@MarkT
Thanks for your reply, i tried that code and no fix.
@opus
Yes you're right. It does have something to do with the IP Address! I tried switching the array around so account(i).ip gets written first and for some reason it is creating a new line.
it is a variable: Dim strName As String and only contains the ip data, e.g. 10.0.0.1. Tested by displaying it in a message box.
Here is what it came out like after i rearranged the array:
Re: [UNRESOLVED] Write To File With Append Creates New Line when it shouldn't
How about this?
Code:
Public Sub write_data()
Dim strData(8) As String
Open App.Path & "\accounts.dat" For Append As #1
For i = 0 To totalaccounts - 1
strData(0) = account(i).firstname
strData(1) = account(i).surname
strData(2) = account(i).username
strData(3) = account(i).password
strData(4) = account(i).email
strData(5) = account(i).compUser
strData(6) = account(i).compName
strData(7) = account(i).ip
strData(7) = Replace(strData(7), vbCr, "")
strData(7) = Replace(strData(7), vbLf, "")
strData(8) = account(i).Date
Print #1, Join(strData, "¿") & vbCrLf
Next i
Close 1
End Sub
Re: [UNRESOLVED] Write To File With Append Creates New Line when it shouldn't
@MarkT
It works! Thanks so much. After reading your code i would never have been able to come up with that... Does that just work by deleting any kind of vbCrLf in the variable? I shall keep that in mind if something similar happens in the future... Thanks so much
@opus
Thanks for your time and your hard work! you helped me a lot!
Re: [RESOLVED] Write To File With Append Creates New Line when it shouldn't
Yes, that is what it does. If you are sure that you have both a carriage return and a line feed at the end of the ip address you could do the replace on one line. Since I wasn’t sure of that I did the replace individually.
Re: [RESOLVED] Write To File With Append Creates New Line when it shouldn't
It's so strange.. is there a way of making sure there is no vbCrLf's in it? I don't understand why there would be one in the ip when it is just numbers and dots.
I thought after saving the string to file correctly as you showed me before, it would read it from the file without any vbCrLf's, but when i try read from the file as Input, there is a new hidden vbCrLf in the middle of the ip, so it thinks the first line of string is just 10.0 instead of 10.0.0.1.
Even though we added it in the text file previously without the carriage return, upon reading it, it seems to gain another one lol. :S so confusing.
Last edited by koushi; Mar 8th, 2011 at 09:20 AM.
Reason: fixing sentence so it is easier to understand
Re: [RESOLVED] Write To File With Append Creates New Line when it shouldn't
Originally Posted by MarkT
What code are you using to read the file?
Keep in mind i swapped the ip position to the start of the string, i thought it might have made things easier?
Edit: I also tried to be smart.. i tried using that replace trick with the vbCr and vbLf but i couldn't work my way around it as the only string it is pulling up as the first line of Input is 10.0
Code:
Public Sub read_data()
If Dir(App.Path & "\accounts.dat") <> "" Then 'if the file exists
totalaccounts = 0
Open App.Path & "\accounts.dat" For Input As #1
Do While Not EOF(1)
Input #1, TempString
TempArray = Split(TempString, "¿")
account(totalaccounts).ip = TempArray(0)
account(totalaccounts).firstname = TempArray(1)
account(totalaccounts).surname = TempArray(2)
account(totalaccounts).username = TempArray(3)
account(totalaccounts).password = TempArray(4)
account(totalaccounts).email = TempArray(5)
account(totalaccounts).compUser = TempArray(6)
account(totalaccounts).compName = TempArray(7)
account(totalaccounts).date = TempArray(8)
totalaccounts = totalaccounts + 1
Loop
Close 1
Else
Open App.Path & "\accounts.dat" For Output As #1 'this will create the file
Close 1
End If
End Sub
Last edited by koushi; Mar 8th, 2011 at 09:41 AM.
Reason: error in code
Re: [RESOLVED] Write To File With Append Creates New Line when it shouldn't
You are saying if you do this it returns the IP address on 2 lines
Code:
Public Sub read_data()
If Dir(App.Path & "\accounts.dat") <> "" Then 'if the file exists
totalaccounts = 0
Open App.Path & "\accounts.dat" For Input As #1
Do While Not EOF(1)
Input #1, TempString
TempArray = Split(TempString, "¿")
account(totalaccounts).ip = TempArray(0)
MsgBox TempArray(0)
account(totalaccounts).firstname = TempArray(1)
account(totalaccounts).surname = TempArray(2)
account(totalaccounts).username = TempArray(3)
account(totalaccounts).password = TempArray(4)
account(totalaccounts).email = TempArray(5)
account(totalaccounts).compUser = TempArray(6)
account(totalaccounts).compName = TempArray(7)
account(totalaccounts).Date = TempArray(8)
totalaccounts = totalaccounts + 1
Loop
Close 1
Else
Open App.Path & "\accounts.dat" For Output As #1 'this will create the file
Close 1
End If
End Sub
Re: [RESOLVED] Write To File With Append Creates New Line when it shouldn't
Originally Posted by MarkT
You are saying if you do this it returns the IP address on 2 lines
If i put in a MsgBox to see what TempString is it displays 10.0 - Well actually it displays 123.211 - (just didn't want to display the full ip) not that it matters.
It doesn't get to the MsgBox in your code because the Split function doesn't actually work as there is no splitter e.g. "¿".
Comes up with an error "Subscript out of range" which makes sense because technically the array doesn't exist.
Re: [RESOLVED] Write To File With Append Creates New Line when it shouldn't
Yes i'll post what it looks like now with MarkT's fix, it writes correctly to file on one line, but when the program is reading the data from file there is now more vbCrLf's hidden somewhere?
I will link what accounts.dat looks like now when written:
Re: [RESOLVED] Write To File With Append Creates New Line when it shouldn't
Originally Posted by opus
How did you declare your UDT holding the account data, especially the .IP part?
This is what i declared everything as:
Code:
Public account(1000) As account
Public Type account
firstname As String
surname As String
username As String
password As String
email As String
compName As String
compUser As String
ip As String
date As Date
End Type