Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


How should I read from a text file if the string contains double quotes?

Posted on 2003-03-03
Medium Priority
Last Modified: 2010-05-01
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!
Question by:bomax
  • 3
  • 3
  • 2
  • +4
LVL 11

Expert Comment

ID: 8062488
Use Write # to write to the file and Input # to read from the file.  Write # take care of all that for you.

Good Luck!


strVar = """Hello"", World!"
boolVar = true
longVar = 1000

Write #1, strVar, boolVar, longVar

Later on....

Input #1, strVar, boolVar, longVar
LVL 55

Expert Comment

by:Ryan Chong
ID: 8062633
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
    On Error Resume Next
    Close #Handle
End Function

Hope this helps
LVL 55

Expert Comment

by:Ryan Chong
ID: 8062638
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 8062721
supunur: I am already using Write and I get this problem.

ryancys: What should I use to retrieve my variables with that function?

Author Comment

ID: 8062728
Anyone have a function to strip the string of it's double quotes before I write the file? that would work too :)
LVL 55

Expert Comment

by:Ryan Chong
ID: 8062792
Try Print instead of Write?

Accepted Solution

navneet77 earned 100 total points
ID: 8062809
You can replace double quotes with single quotes before writing to the file by using

Write #1, Replace(strToWrite,"""", "'")

Expert Comment

ID: 8063254
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.

Expert Comment

ID: 8063777
U can read it as Line Input #1,msg

Expert Comment

ID: 8063783
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

Expert Comment

ID: 8066156
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

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
       end if
    end if


Expert Comment

ID: 8066175
missed the line...
nWrite = FreeFile
same as the second...
nRead = FreeFile

Author Comment

ID: 8067246
This works okay...

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question