bomax
asked on
How should I read from a text file if the string contains double quotes?
I am writing a variable to a file which contains several quotes. When I try to read back the file using Input# it sees these quotes and thinks they signal the end of a variable when in fact they do not. How should I get around this? Thank you!
Try use this function:
Public Function ReadFileText(ByVal FileName As String) As String
On Error GoTo EHandler
Dim Handle As Integer
Handle = FreeFile
Open FileName For Input As #Handle
ReadFileText = Input$(LOF(Handle), Handle)
On Error Resume Next
Close #Handle
Exit Function
EHandler:
ShowErrMsg
On Error Resume Next
Close #Handle
End Function
Hope this helps
Public Function ReadFileText(ByVal FileName As String) As String
On Error GoTo EHandler
Dim Handle As Integer
Handle = FreeFile
Open FileName For Input As #Handle
ReadFileText = Input$(LOF(Handle), Handle)
On Error Resume Next
Close #Handle
Exit Function
EHandler:
ShowErrMsg
On Error Resume Next
Close #Handle
End Function
Hope this helps
Optional code to show an error:
Public Sub ShowErrMsg(Optional ShowErrNumber As Boolean = True, Optional ShowErrSource As Boolean = True)
Dim ErrMsg As String, ErrTitle As String
ErrMsg = IIf(ShowErrNumber = True, Err.Number & ": ", "") & Err.Description
ErrTitle = "Error" & IIf(ShowErrSource = True, ": " & Err.Source, "")
MsgBox ErrMsg, vbCritical, ErrTitle
End Sub
Public Sub ShowErrMsg(Optional ShowErrNumber As Boolean = True, Optional ShowErrSource As Boolean = True)
Dim ErrMsg As String, ErrTitle As String
ErrMsg = IIf(ShowErrNumber = True, Err.Number & ": ", "") & Err.Description
ErrTitle = "Error" & IIf(ShowErrSource = True, ": " & Err.Source, "")
MsgBox ErrMsg, vbCritical, ErrTitle
End Sub
ASKER
supunur: I am already using Write and I get this problem.
ryancys: What should I use to retrieve my variables with that function?
ryancys: What should I use to retrieve my variables with that function?
ASKER
Anyone have a function to strip the string of it's double quotes before I write the file? that would work too :)
Try Print instead of Write?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
When you save, just replace the quotes with a macro, like %Q, using the replace method. Then, when you open, replace all the macros (%Q) with quotes.
U can read it as Line Input #1,msg
U can read it as Line Input #1,msg
This will return the text with quotes and all punctuation marks and signs everything in that line
This will return the text with quotes and all punctuation marks and signs everything in that line
Or as I like to do, open the file for binary and read the entire file into a variable, split the file contents and work with each line as an array subscript
ie:
Public Type empRecord
name as string
email as string
end Type
Public Function getRecordByName(FileName As String, empName As String) As empRecord
Dim nRead As Integer, a As Variant, sFileContents As String, lX As Long, empToReturn As empRecord
nRead = FreeFile
Open filename for binary access read lock read write as #nRead
sFileContents = Space(LOF(nRead))
Get #nRead, , sFileContents
Close #nRead
' then
a = Split(sFileContents, vbCrLf)
' search for record
For lX = 0 To UBound(a)
Dim empRec As Variant
empRec = Split(a(lX), ",")
If empRec(0) = empName Then
empToReturn.name = empName
empToReturn.email = empRec(1)
Exit For
end if
Next lX
getRecordByName = empToReturn
End Function
' the a() array could be global or not
' changes to the records can be made by
' splitting the array value changing the value then
' using Join to ... join it up again
' but to save the changes would be:
dim newContents As String
open filename for binary read lock read write as #nRead
sNewContents = Space(LOF(nRead))
Get #nRead, , sNewContents
close #nRead
if sNewContents <> sFileContents then
' contents changed from another source
if msgbox "Yes?", vbYesNo then
Kill filename ' begin anew
dim nWrite as integer
sNewContents = Join(a, vbCrLf)
open filename for binary _
access write lock read write as #nWrite
Put #nWrite, , sNewContents
Close #nWrite
else
end if
end if
ie:
Public Type empRecord
name as string
email as string
end Type
Public Function getRecordByName(FileName As String, empName As String) As empRecord
Dim nRead As Integer, a As Variant, sFileContents As String, lX As Long, empToReturn As empRecord
nRead = FreeFile
Open filename for binary access read lock read write as #nRead
sFileContents = Space(LOF(nRead))
Get #nRead, , sFileContents
Close #nRead
' then
a = Split(sFileContents, vbCrLf)
' search for record
For lX = 0 To UBound(a)
Dim empRec As Variant
empRec = Split(a(lX), ",")
If empRec(0) = empName Then
empToReturn.name = empName
empToReturn.email = empRec(1)
Exit For
end if
Next lX
getRecordByName = empToReturn
End Function
' the a() array could be global or not
' changes to the records can be made by
' splitting the array value changing the value then
' using Join to ... join it up again
' but to save the changes would be:
dim newContents As String
open filename for binary read lock read write as #nRead
sNewContents = Space(LOF(nRead))
Get #nRead, , sNewContents
close #nRead
if sNewContents <> sFileContents then
' contents changed from another source
if msgbox "Yes?", vbYesNo then
Kill filename ' begin anew
dim nWrite as integer
sNewContents = Join(a, vbCrLf)
open filename for binary _
access write lock read write as #nWrite
Put #nWrite, , sNewContents
Close #nWrite
else
end if
end if
missed the line...
nWrite = FreeFile
same as the second...
nRead = FreeFile
nWrite = FreeFile
same as the second...
nRead = FreeFile
ASKER
This works okay...
Good Luck!
eg.
strVar = """Hello"", World!"
boolVar = true
longVar = 1000
Write #1, strVar, boolVar, longVar
....
Later on....
Input #1, strVar, boolVar, longVar