|
-
Jun 11th, 2005, 04:13 AM
#1
Thread Starter
Addicted Member
Huge txt-saving problem!
Whenever I go to save files with this code:
VB Code:
Sub SaveText(txtSave As TextBox, Path As String)
Dim TextString As String
On Error Resume Next
TextString$ = txtSave.Text
Open Path$ For Output As #1
Print #1, TextString$
Close #1
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?
-
Jun 11th, 2005, 04:22 AM
#2
Thread Starter
Addicted Member
Re: Huge txt-saving problem!
-
Jun 11th, 2005, 04:23 AM
#3
Re: Huge txt-saving problem!
VB Code:
Sub SaveText(txtSave As TextBox, Path As String)
Dim TextString As String
On Error Resume Next
TextString$ = txtSave.Text
Open Path$ For Output As #1
Print #1, [B]left(TextString$, len(textstring) - 2) [/B]
Close #1
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
-
Jun 11th, 2005, 04:24 AM
#4
Thread Starter
Addicted Member
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:
Text1.Text = "hello"
SaveText Text1, thepath
-
Jun 11th, 2005, 04:26 AM
#5
Re: Huge txt-saving problem!
did you try it??
it is likely the last 2 characters are vbnewline
pete
-
Jun 11th, 2005, 04:28 AM
#6
Thread Starter
Addicted Member
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?
-
Jun 11th, 2005, 04:30 AM
#7
Re: Huge txt-saving problem!
i use it all the time without problem
post your project if you want me to try it
pete
-
Jun 11th, 2005, 04:59 AM
#8
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:
Sub SaveText(txtSave As TextBox, Path As String)
Dim TextString As String
On Error Resume Next
TextString$ = txtSave.Text
Open Path$ For Output As #1
Print #1, TextString$; '<-- Append semicolon here
Close #1
End Sub
Pradeep
-
Jun 11th, 2005, 04:59 AM
#9
Thread Starter
Addicted Member
Re: Huge txt-saving problem!
theres no use, my whole project is that one function, a button, and those 2 lines.
VB Code:
Sub SaveText(txtSave As TextBox, Path As String)
Dim TextString As String
On Error Resume Next
TextString$ = txtSave.Text
Open Path$ For Output As #1
Print #1, TextString$
Close #1
End Sub
Private Sub Command1_Click()
Text1.Text = "hello"
SaveText Text1, thepath
End Sub
-
Jun 11th, 2005, 05:19 AM
#10
Re: Huge txt-saving problem!
try this
Print #1, TextString$;
pete
-
Jun 11th, 2005, 04:16 PM
#11
Re: Huge txt-saving problem!
Just open the file as Binary...
VB Code:
Sub SaveText(txtSave As TextBox, Path As String)
Dim TextString As String
On Error Resume Next
TextString = txtSave.Text
Open Path For Binary Access Write Lock Write As #1
Put #1, , TextString
Close #1
End Sub
Private Sub Command1_Click()
Text1.Text = "hello"
SaveText Text1, thepath
End Sub
I never EVER open files other than Binary, and I can do everything I need to do in Binary mode...
-
Jun 11th, 2005, 04:55 PM
#12
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.
-
Jun 11th, 2005, 05:38 PM
#13
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:
Option Explicit
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
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
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
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
Public Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Public Const GENERIC_READ = &H80000000
Public Const GENERIC_WRITE = &H40000000
Public Const FILE_SHARE_READ = &H1
Public Const FILE_SHARE_WRITE = &H2
Public Const CREATE_ALWAYS = 2
Public Const CREATE_NEW = 1
Public Const OPEN_ALWAYS = 4
Public Const OPEN_EXISTING = 3
Public Const TRUNCATE_EXISTING = 5
Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
Public Const FILE_ATTRIBUTE_HIDDEN = &H2
Public Const FILE_ATTRIBUTE_NORMAL = &H80
Public Const FILE_ATTRIBUTE_READONLY = &H1
Public Const FILE_ATTRIBUTE_SYSTEM = &H4
Public Const FILE_FLAG_DELETE_ON_CLOSE = &H4000000
Public Const FILE_FLAG_NO_BUFFERING = &H20000000
Public Const FILE_FLAG_OVERLAPPED = &H40000000
Public Const FILE_FLAG_POSIX_SEMANTICS = &H1000000
Public Const FILE_FLAG_RANDOM_ACCESS = &H10000000
Public Const FILE_FLAG_SEQUENTIAL_SCAN = &H8000000
Public Const FILE_FLAG_WRITE_THROUGH = &H80000000
Public Function ReadFileAPI(File As String) As String
Dim filesizelow As Long
Dim filesizehigh As Long
Dim longbuffer As Long
Dim stringbuffer As String
Dim numread As Long
Dim hFile As Long
Dim retval As Long
On Error Resume Next
hFile = CreateFileNS(File, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
'get an handle for the file
If hFile = -1 Then
'there is an error! maybe the file doesn't exist
ReadFileAPI = "-1"
Exit Function
End If
filesizelow = GetFileSize(hFile, filesizehigh)
'get the size of the file
stringbuffer = Space(filesizelow)
'file a string with spaces
retval = ReadFileNO(hFile, ByVal stringbuffer, filesizelow, numread, 0)
'get the whole data of the file
If numread = 1 Then
'if numread is 1 then everything is ok!
ReadFileAPI = stringbuffer
Else
ReadFileAPI = -1
End If
retval = CloseHandle(hFile)
'important: close filehandle!
End Function
Public Function FileCleanWrite(FilePath As String, Data As String) As Long
'Saves files without adding extra bytes to the front or end of the file
Dim hFile As Long
Dim lBytesWritten As Long
Dim sTemp As String
'This API does not fail gracefully, so be careful
On Error GoTo Erro:
If Len(FilePath) = 0 Then
MsgBox "No Path Given"
Exit Function
Else
sTemp = Left$(FilePath, InStrRev(FilePath, "\"))
If Len(Dir(sTemp, vbDirectory)) = 0 Then
MsgBox "Directory does not exist"
Exit Function
ElseIf Len(Dir(FilePath)) = 0 Then
'It's a new file, create a 'seed' file
FileSaveString FilePath, ""
End If
End If
hFile = CreateFileNS(FilePath, GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, 0)
'get an handle for the file
If hFile = -1 Then
'there is still an error!
FileCleanWrite = "-1"
CloseHandle hFile
Exit Function
End If
'write the data
WriteFileNO hFile, ByVal Data, Len(Data), lBytesWritten, 0
'returns the number of written bytes
FileCleanWrite = lBytesWritten
'important: close filehandle!
Erro:
CloseHandle hFile
End Function
-
Jun 11th, 2005, 07:30 PM
#14
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
-
Jun 11th, 2005, 07:38 PM
#15
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.
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
|