Results 1 to 21 of 21

Thread: [RESOLVED] Write To File With Append Creates New Line when it shouldn't

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    Resolved [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:

    Code:
    john¿doe¿johnny¿password¿john@hotmail.com¿John¿JOHN-PC¿10.0.0.1
    ¿8/03/2011
    as you can see the date is on a new line, but by using "Append" instead of "Output" and the ";" should keep it on the same line.. hmm
    Last edited by koushi; Mar 8th, 2011 at 08:45 AM. Reason: adding a code snippet

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Write to File, When appending, goes to new line Problem

    ?? Using WordWrap to OFF in NotePad will show all entries in a single line to me ???
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    Re: Write to File, When appending, goes to new line Problem

    Quote Originally Posted by opus View Post
    ?? 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

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Write to File, When appending, goes to new line Problem

    Please post an example of your output-file ("accounts.dat").
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    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
    Attached Files Attached Files

  6. #6
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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

  7. #7
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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


    Wait, I'm too old to hurry!

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    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:

    Code:
    10.0.0.1
    ¿John¿Doe¿Johnny¿password¿john@hotmail.com¿John¿JOHN-PC¿8/03/2011

  9. #9
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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

  10. #10

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    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!

  11. #11
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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.

  12. #12

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    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

  13. #13
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: [RESOLVED] Write To File With Append Creates New Line when it shouldn't

    What code are you using to read the file?

  14. #14

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    Re: [RESOLVED] Write To File With Append Creates New Line when it shouldn't

    Quote Originally Posted by MarkT View Post
    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, "&#191;")
                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

  15. #15
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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, "&#191;")
                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

  16. #16

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    Re: [RESOLVED] Write To File With Append Creates New Line when it shouldn't

    Quote Originally Posted by MarkT View Post
    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.

  17. #17
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [RESOLVED] Write To File With Append Creates New Line when it shouldn't

    Just to make sure, did you have a look at the actual written file using notepad?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  18. #18

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    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:

    Code:
    10.0.0.1&#191;John&#191;Doe&#191;Johnny&#191;password&#191;john@hotmail.com&#191;John&#191;JOHN-PC&#191;8/03/2011

    But when reading from the same file, the first line it reads is 10.0 and that's all...

  19. #19
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [RESOLVED] Write To File With Append Creates New Line when it shouldn't

    How did you declare your UDT holding the account data, especially the .IP part?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  20. #20

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    Re: [RESOLVED] Write To File With Append Creates New Line when it shouldn't

    Quote Originally Posted by opus View Post
    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

  21. #21

    Thread Starter
    Member
    Join Date
    Apr 2010
    Location
    Gold Coast, Australia
    Posts
    43

    Re: [RESOLVED] Write To File With Append Creates New Line when it shouldn't

    i give up i just took the whole ip part out lol

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width