Results 1 to 15 of 15

Thread: Huge txt-saving problem!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Huge txt-saving problem!

    Whenever I go to save files with this code:

    VB Code:
    1. Sub SaveText(txtSave As TextBox, Path As String)
    2.     Dim TextString As String
    3.     On Error Resume Next
    4.     TextString$ = txtSave.Text
    5.     Open Path$ For Output As #1
    6.     Print #1, TextString$
    7.     Close #1
    8. End Sub

    It adds the right data but at the very end it adds a vbNewLine! This messes up everything and i must prevent it from doing that.

    Example, if i try to save "hello" to a file, it adds this:

    "hello
    "

    big problem! can this be solved?

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Re: Huge txt-saving problem!


  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Huge txt-saving problem!

    VB Code:
    1. Sub SaveText(txtSave As TextBox, Path As String)
    2.     Dim TextString As String
    3.     On Error Resume Next
    4.     TextString$ = txtSave.Text
    5.     Open Path$ For Output As #1
    6.     Print #1, [B]left(TextString$, len(textstring) - 2) [/B]
    7.     Close #1
    8. End Sub

    takes of 2 character from the end of the string, if you loose the last character of your text change to 1

    pete

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Re: Huge txt-saving problem!

    taking 2 character away will make it 'hel' instead of 'hello'. thats not what i want.

    my string is a simple "hello" and thats all. its not "hello" & vbcrlf, its not "hello" & vbnewline, its "hello".

    VB Code:
    1. Text1.Text = "hello"
    2. SaveText Text1, thepath

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Huge txt-saving problem!

    did you try it??

    it is likely the last 2 characters are vbnewline

    pete

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Re: Huge txt-saving problem!

    yes i tried it! the last 2 characters is not a vbnewline i am 100 PERCENT SURE.

    the Print function seems to add a new line at the end, try it yourself! is there any other way then Print?

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Huge txt-saving problem!

    i use it all the time without problem

    post your project if you want me to try it

    pete

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Huge txt-saving problem!

    Yes, that's one bad thing VB does.

    You just need to add a semicolon [;] after print statement. This tricks VB into thinking that it will be recieveing something more and won't put that new line character.
    VB Code:
    1. Sub SaveText(txtSave As TextBox, Path As String)
    2.     Dim TextString As String
    3.     On Error Resume Next
    4.     TextString$ = txtSave.Text
    5.     Open Path$ For Output As #1
    6.     Print #1, TextString$; '<-- Append semicolon here
    7.     Close #1
    8. End Sub

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Location
    Cleveland, Ohio
    Posts
    255

    Re: Huge txt-saving problem!

    theres no use, my whole project is that one function, a button, and those 2 lines.

    VB Code:
    1. Sub SaveText(txtSave As TextBox, Path As String)
    2.     Dim TextString As String
    3.     On Error Resume Next
    4.     TextString$ = txtSave.Text
    5.     Open Path$ For Output As #1
    6.     Print #1, TextString$
    7.     Close #1
    8. End Sub
    9.  
    10. Private Sub Command1_Click()
    11. Text1.Text = "hello"
    12. SaveText Text1, thepath
    13. End Sub

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Huge txt-saving problem!

    try this

    Print #1, TextString$;

    pete

  11. #11
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Huge txt-saving problem!

    Just open the file as Binary...
    VB Code:
    1. Sub SaveText(txtSave As TextBox, Path As String)
    2.     Dim TextString As String
    3.    
    4.     On Error Resume Next
    5.     TextString = txtSave.Text
    6.    
    7.     Open Path For Binary Access Write Lock Write As #1
    8.     Put #1, , TextString
    9.     Close #1
    10. End Sub
    11.  
    12. Private Sub Command1_Click()
    13.     Text1.Text = "hello"
    14.     SaveText Text1, thepath
    15. End Sub
    I never EVER open files other than Binary, and I can do everything I need to do in Binary mode...

  12. #12
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Huge txt-saving problem!

    Another option is to use FSO - the file system object. Some here on the forum dislike it, but it also allows for tighter control over output. Not sure it will suppress the CR/LF line delimiter - but I would guess it does.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  13. #13
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Huge txt-saving problem!

    I ran into the same problem when saving pics.
    There a are 2 ways around it (that I know of)

    1. Use a RichTextBox to save the file.

    or
    2. use the next module

    VB Code:
    1. Option Explicit
    2. Public Declare Function ReadFileNO Lib "kernel32" Alias "ReadFile" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
    3. Public Declare Function CreateFileNS Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    4. Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    5. Public Declare Function WriteFileNO Lib "kernel32" Alias "WriteFile" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
    6. Public Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
    7.  
    8.  
    9. Public Const GENERIC_READ = &H80000000
    10. Public Const GENERIC_WRITE = &H40000000
    11. Public Const FILE_SHARE_READ = &H1
    12. Public Const FILE_SHARE_WRITE = &H2
    13. Public Const CREATE_ALWAYS = 2
    14. Public Const CREATE_NEW = 1
    15. Public Const OPEN_ALWAYS = 4
    16. Public Const OPEN_EXISTING = 3
    17. Public Const TRUNCATE_EXISTING = 5
    18. Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
    19. Public Const FILE_ATTRIBUTE_HIDDEN = &H2
    20. Public Const FILE_ATTRIBUTE_NORMAL = &H80
    21. Public Const FILE_ATTRIBUTE_READONLY = &H1
    22. Public Const FILE_ATTRIBUTE_SYSTEM = &H4
    23. Public Const FILE_FLAG_DELETE_ON_CLOSE = &H4000000
    24. Public Const FILE_FLAG_NO_BUFFERING = &H20000000
    25. Public Const FILE_FLAG_OVERLAPPED = &H40000000
    26. Public Const FILE_FLAG_POSIX_SEMANTICS = &H1000000
    27. Public Const FILE_FLAG_RANDOM_ACCESS = &H10000000
    28. Public Const FILE_FLAG_SEQUENTIAL_SCAN = &H8000000
    29. Public Const FILE_FLAG_WRITE_THROUGH = &H80000000
    30.  
    31. Public Function ReadFileAPI(File As String) As String
    32.     Dim filesizelow As Long
    33.     Dim filesizehigh As Long
    34.     Dim longbuffer As Long
    35.     Dim stringbuffer As String
    36.     Dim numread As Long
    37.     Dim hFile As Long
    38.     Dim retval As Long
    39.    
    40.     On Error Resume Next
    41.     hFile = CreateFileNS(File, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
    42.         'get an handle for the file
    43.     If hFile = -1 Then
    44.         'there is an error! maybe the file doesn't exist
    45.         ReadFileAPI = "-1"
    46.         Exit Function
    47.     End If
    48.    
    49.     filesizelow = GetFileSize(hFile, filesizehigh)
    50.         'get the size of the file
    51.     stringbuffer = Space(filesizelow)
    52.         'file a string with spaces
    53.     retval = ReadFileNO(hFile, ByVal stringbuffer, filesizelow, numread, 0)
    54.         'get the whole data of the file
    55.     If numread = 1 Then
    56.         'if numread is 1 then everything is ok!
    57.        ReadFileAPI = stringbuffer
    58.     Else
    59.         ReadFileAPI = -1
    60.     End If
    61.    
    62.     retval = CloseHandle(hFile)
    63.     'important: close filehandle!
    64. End Function
    65.  
    66. Public Function FileCleanWrite(FilePath As String, Data As String) As Long
    67. 'Saves files without adding extra bytes to the front or end of the file
    68.     Dim hFile As Long
    69.     Dim lBytesWritten As Long
    70.     Dim sTemp As String
    71.    
    72.     'This API does not fail gracefully, so be careful
    73.     On Error GoTo Erro:
    74.     If Len(FilePath) = 0 Then
    75.         MsgBox "No Path Given"
    76.         Exit Function
    77.     Else
    78.         sTemp = Left$(FilePath, InStrRev(FilePath, "\"))
    79.         If Len(Dir(sTemp, vbDirectory)) = 0 Then
    80.             MsgBox "Directory does not exist"
    81.             Exit Function
    82.         ElseIf Len(Dir(FilePath)) = 0 Then
    83.             'It's a new file, create a 'seed' file
    84.             FileSaveString FilePath, ""
    85.         End If
    86.     End If
    87.     hFile = CreateFileNS(FilePath, GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, 0)
    88.         'get an handle for the file
    89.     If hFile = -1 Then
    90.         'there is still an error!
    91.         FileCleanWrite = "-1"
    92.         CloseHandle hFile
    93.         Exit Function
    94.     End If
    95.    
    96.     'write the data
    97.     WriteFileNO hFile, ByVal Data, Len(Data), lBytesWritten, 0
    98.     'returns the number of written bytes
    99.     FileCleanWrite = lBytesWritten
    100.     'important: close filehandle!
    101. Erro:
    102.     CloseHandle hFile
    103.  
    104. End Function

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Huge txt-saving problem!

    if you put a ; at the end of your print statement the printing continues on the same line, the next print statement wll not be on a new line, at the end of file there will not be a blank line

    pete

  15. #15
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Huge txt-saving problem!

    westconn1 - I agree that putting a ; will cause no CR/LF...

    But you still run into the "logical line width" issue with VB. At some specific width - 132, 512, 1000 - not sure what it is with VB - it will want to wrap the text.

    That's why with output files that regularly exceed 1000 bytes of width, we use FSO.

    For small TDF text files, we use PRINT # with ; to suppress CR/FL.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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