?
Solved

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

Posted on 2003-03-03
13
Medium Priority
?
150 Views
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!
0
Comment
Question by:bomax
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +4
13 Comments
 
LVL 11

Expert Comment

by:supunr
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!

eg.

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

Write #1, strVar, boolVar, longVar
....


Later on....

Input #1, strVar, boolVar, longVar
0
 
LVL 53

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
EHandler:
    ShowErrMsg
    On Error Resume Next
    Close #Handle
End Function

Hope this helps
0
 
LVL 53

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
0
Technology Partners: 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

by:bomax
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?
0
 

Author Comment

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

Expert Comment

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

Accepted Solution

by:
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,"""", "'")
0
 

Expert Comment

by:fakehampster
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.
0
 
LVL 5

Expert Comment

by:avya2k
ID: 8063777
U can read it as Line Input #1,msg
0
 
LVL 5

Expert Comment

by:avya2k
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
0
 

Expert Comment

by:qwasted
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

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

0
 

Expert Comment

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

Author Comment

by:bomax
ID: 8067246
This works okay...
0

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

771 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